Windows: convert buf_IncrSyncer to pthreads
[openafs.git] / src / WINNT / afsd / cm_buf.c
index b9ea1cb..96f9f67 100644 (file)
@@ -337,7 +337,8 @@ buf_Sync(int quitOnShutdown)
 }
 
 /* incremental sync daemon.  Writes all dirty buffers every 5000 ms */
-void buf_IncrSyncer(long parm)
+static void *
+buf_IncrSyncer(void * parm)
 {
     long wasDirty = 0;
     long i;
@@ -353,6 +354,9 @@ void buf_IncrSyncer(long parm)
 
         wasDirty = buf_Sync(1);
     } /* whole daemon's while loop */
+
+    pthread_exit(NULL);
+    return NULL;
 }
 
 long
@@ -461,9 +465,10 @@ long buf_Init(int newFile, cm_buf_ops_t *opsp, afs_uint64 nbuffers)
 {
     static osi_once_t once;
     cm_buf_t *bp;
-    thread_t phandle;
+    pthread_t phandle;
+    pthread_attr_t tattr;
+    int pstatus;
     long i;
-    unsigned long pid;
     char *data;
 
     if ( newFile ) {
@@ -488,7 +493,7 @@ long buf_Init(int newFile, cm_buf_ops_t *opsp, afs_uint64 nbuffers)
             cm_data.buf_nOrigBuffers = cm_data.buf_nbuffers;
 
             /* lower hash size to a prime number */
-           cm_data.buf_hashSize = osi_PrimeLessThan((afs_uint32)(cm_data.buf_nbuffers/7 + 1));
+           cm_data.buf_hashSize = cm_NextHighestPowerOf2((afs_uint32)(cm_data.buf_nbuffers/7));
 
             /* create hash table */
             memset((void *)cm_data.buf_scacheHashTablepp, 0, cm_data.buf_hashSize * sizeof(cm_buf_t *));
@@ -602,12 +607,13 @@ long buf_Init(int newFile, cm_buf_ops_t *opsp, afs_uint64 nbuffers)
         osi_EndOnce(&once);
 
         /* and create the incr-syncer */
-        phandle = thrd_Create(0, 0,
-                               (ThreadFunc) buf_IncrSyncer, 0, 0, &pid,
-                               "buf_IncrSyncer");
+        pthread_attr_init(&tattr);
+        pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED);
+
+        pstatus = pthread_create(&phandle, &tattr, buf_IncrSyncer, 0);
+        osi_assertx(pstatus == 0, "buf: can't create incremental sync proc");
 
-        osi_assertx(phandle != NULL, "buf: can't create incremental sync proc");
-        CloseHandle(phandle);
+        pthread_attr_destroy(&tattr);
     }
 
 #ifdef TESTING
@@ -1898,6 +1904,7 @@ long buf_Truncate(cm_scache_t *scp, cm_user_t *userp, cm_req_t *reqp,
     long code;
     long bufferPos;
     afs_uint32 i;
+    afs_uint32 invalidate = 0;
 
     /* assert that cm_bufCreateLock is held in write mode */
     lock_AssertWrite(&scp->bufCreateLock);
@@ -1968,10 +1975,7 @@ long buf_Truncate(cm_scache_t *scp, cm_user_t *userp, cm_req_t *reqp,
                         lock_ReleaseWrite(&buf_globalLock);
                     }
                 } else {
-                    if (RDR_Initialized)
-                        RDR_InvalidateObject(scp->fid.cell, scp->fid.volume, scp->fid.vnode,
-                                             scp->fid.unique, scp->fid.hash,
-                                             scp->fileType, AFS_INVALIDATE_SMB);
+                    invalidate = 1;
                 }
                 _InterlockedAnd(&bufp->flags, ~CM_BUF_DIRTY);
                 bufp->error = 0;
@@ -2020,6 +2024,11 @@ long buf_Truncate(cm_scache_t *scp, cm_user_t *userp, cm_req_t *reqp,
     buf_ValidateBufQueues();
 #endif /* TESTING */
 
+    if (invalidate && RDR_Initialized)
+        RDR_InvalidateObject(scp->fid.cell, scp->fid.volume, scp->fid.vnode,
+                             scp->fid.unique, scp->fid.hash,
+                             scp->fileType, AFS_INVALIDATE_SMB);
+
     /* done */
     return code;
 }