Fossil

Check-in [3c084879]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Improve Win32 error handling in the backoffice module and fix a compiler warning.
Downloads: Tarball | ZIP archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 3c0848797d1c7e1eb89b7c8f387c53ad75d675f2a8598c3b4ca53d5e73df1b43
User & Date: mistachkin 2018-08-07 23:08:27.335
Context
2018-08-07
23:16
Change backoffice-nodelay to default off. ... (check-in: 12c487c4 user: drh tags: trunk)
23:08
Improve Win32 error handling in the backoffice module and fix a compiler warning. ... (check-in: 3c084879 user: mistachkin tags: trunk)
23:03
Make it possible to debug child processes on Windows. ... (check-in: e285341f user: mistachkin tags: trunk)
Changes
Unified Diff Ignore Whitespace Patch
Changes to src/backoffice.c.
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295













296
297
298




299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
*/
static void backofficeSigalrmHandler(int x){
  fossil_panic("backoffice timeout (%d seconds)", x);
}
#if defined(_WIN32)
static void *threadHandle = NULL;
static void __stdcall backofficeWin32NoopApcProc(ULONG_PTR pArg){} /* NO-OP */
static void backofficeWin32ThreadCleanup(){
  if( threadHandle!=NULL ){
    /* Queue no-op asynchronous procedure call to the sleeping
     * thread.  This will cause it to wake up with a non-zero
     * return value. */
    if( QueueUserAPC(backofficeWin32NoopApcProc, threadHandle, 0) ){
      /* Wait for the thread to wake up and then exit. */
      WaitForSingleObject(threadHandle, INFINITE);













    }
    CloseHandle(threadHandle);
    threadHandle = NULL;




  }
}
static unsigned __stdcall backofficeWin32SigalrmThreadProc(
  void *pArg /* IN: Pointer to integer number of whole seconds. */
){
  int seconds = FOSSIL_PTR_TO_INT(pArg);
  if( SleepEx((DWORD)seconds * 1000, TRUE)==0 ){
    backofficeSigalrmHandler(seconds);
  }
  _endthreadex(0);
  return 0; /* NOT REACHED */
}
#endif
static void backofficeTimeout(int x){
#if defined(_WIN32)
  backofficeWin32ThreadCleanup();
  threadHandle = (void*)_beginthreadex(
    0, 0, backofficeWin32SigalrmThreadProc, FOSSIL_INT_TO_PTR(x), 0, 0
  );
#else
  signal(SIGALRM, backofficeSigalrmHandler);
  alarm(x);
#endif







|







>
>
>
>
>
>
>
>
>
>
>
>
>



>
>
>
>















|







281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
*/
static void backofficeSigalrmHandler(int x){
  fossil_panic("backoffice timeout (%d seconds)", x);
}
#if defined(_WIN32)
static void *threadHandle = NULL;
static void __stdcall backofficeWin32NoopApcProc(ULONG_PTR pArg){} /* NO-OP */
static void backofficeWin32ThreadCleanup(int bStrict){
  if( threadHandle!=NULL ){
    /* Queue no-op asynchronous procedure call to the sleeping
     * thread.  This will cause it to wake up with a non-zero
     * return value. */
    if( QueueUserAPC(backofficeWin32NoopApcProc, threadHandle, 0) ){
      /* Wait for the thread to wake up and then exit. */
      WaitForSingleObject(threadHandle, INFINITE);
    }else if(bStrict){
      DWORD dwLastError = GetLastError();
      fossil_errorlog(
        "backofficeWin32ThreadCleanup: QueueUserAPC failed, code %lu",
        dwLastError
      );
      if( !TerminateThread(threadHandle, dwLastError) ){
        dwLastError = GetLastError();
        fossil_panic(
          "backofficeWin32ThreadCleanup: TerminateThread failed, code %lu",
          dwLastError
        );
      }
    }
    CloseHandle(threadHandle);
    threadHandle = NULL;
  }else if(bStrict){
    fossil_panic(
      "backofficeWin32ThreadCleanup: no timeout thread handle"
    );
  }
}
static unsigned __stdcall backofficeWin32SigalrmThreadProc(
  void *pArg /* IN: Pointer to integer number of whole seconds. */
){
  int seconds = FOSSIL_PTR_TO_INT(pArg);
  if( SleepEx((DWORD)seconds * 1000, TRUE)==0 ){
    backofficeSigalrmHandler(seconds);
  }
  _endthreadex(0);
  return 0; /* NOT REACHED */
}
#endif
static void backofficeTimeout(int x){
#if defined(_WIN32)
  backofficeWin32ThreadCleanup(0);
  threadHandle = (void*)_beginthreadex(
    0, 0, backofficeWin32SigalrmThreadProc, FOSSIL_INT_TO_PTR(x), 0, 0
  );
#else
  signal(SIGALRM, backofficeSigalrmHandler);
  alarm(x);
#endif
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
        }
        db_end_transaction(0);
        break;
      }
    }
  }
#if defined(_WIN32)
  assert( threadHandle!=NULL );
  backofficeWin32ThreadCleanup();
#endif
  return;
}

/*
** This routine runs to do the backoffice processing.  When adding new
** backoffice processing tasks, add them here.







<
|







495
496
497
498
499
500
501

502
503
504
505
506
507
508
509
        }
        db_end_transaction(0);
        break;
      }
    }
  }
#if defined(_WIN32)

  backofficeWin32ThreadCleanup(1);
#endif
  return;
}

/*
** This routine runs to do the backoffice processing.  When adding new
** backoffice processing tasks, add them here.
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
    wchar_t *ax[5];
    argv[0] = g.nameOfExe;
    argv[1] = "backoffice";
    argv[2] = "-R";
    argv[3] = backofficeDb;
    ax[4] = 0;
    for(i=0; i<=3; i++) ax[i] = fossil_utf8_to_unicode(argv[i]);
    x = _wspawnv(_P_NOWAIT, ax[0], ax);
    for(i=0; i<=3; i++) fossil_unicode_free(ax[i]);
    if( g.fAnyTrace ){
      fprintf(stderr, 
        "/***** Subprocess %d creates backoffice child %d *****/\n",
        GETPID(), (int)x);
    }
    if( x>=0 ) return;







|







559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
    wchar_t *ax[5];
    argv[0] = g.nameOfExe;
    argv[1] = "backoffice";
    argv[2] = "-R";
    argv[3] = backofficeDb;
    ax[4] = 0;
    for(i=0; i<=3; i++) ax[i] = fossil_utf8_to_unicode(argv[i]);
    x = _wspawnv(_P_NOWAIT, ax[0], (const wchar_t * const *)ax);
    for(i=0; i<=3; i++) fossil_unicode_free(ax[i]);
    if( g.fAnyTrace ){
      fprintf(stderr, 
        "/***** Subprocess %d creates backoffice child %d *****/\n",
        GETPID(), (int)x);
    }
    if( x>=0 ) return;