Header And Logo

PostgreSQL
| The world's most advanced open source database.

timer.c

Go to the documentation of this file.
00001 /*-------------------------------------------------------------------------
00002  *
00003  * timer.c
00004  *    Microsoft Windows Win32 Timer Implementation
00005  *
00006  *    Limitations of this implementation:
00007  *
00008  *    - Does not support interval timer (value->it_interval)
00009  *    - Only supports ITIMER_REAL
00010  *
00011  * Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group
00012  *
00013  * IDENTIFICATION
00014  *    src/backend/port/win32/timer.c
00015  *
00016  *-------------------------------------------------------------------------
00017  */
00018 
00019 #include "postgres.h"
00020 
00021 
00022 /* Communication area for inter-thread communication */
00023 typedef struct timerCA
00024 {
00025     struct itimerval value;
00026     HANDLE      event;
00027     CRITICAL_SECTION crit_sec;
00028 } timerCA;
00029 
00030 static timerCA timerCommArea;
00031 static HANDLE timerThreadHandle = INVALID_HANDLE_VALUE;
00032 
00033 
00034 /* Timer management thread */
00035 static DWORD WINAPI
00036 pg_timer_thread(LPVOID param)
00037 {
00038     DWORD       waittime;
00039 
00040     Assert(param == NULL);
00041 
00042     waittime = INFINITE;
00043 
00044     for (;;)
00045     {
00046         int         r;
00047 
00048         r = WaitForSingleObjectEx(timerCommArea.event, waittime, FALSE);
00049         if (r == WAIT_OBJECT_0)
00050         {
00051             /* Event signalled from main thread, change the timer */
00052             EnterCriticalSection(&timerCommArea.crit_sec);
00053             if (timerCommArea.value.it_value.tv_sec == 0 &&
00054                 timerCommArea.value.it_value.tv_usec == 0)
00055                 waittime = INFINITE;    /* Cancel the interrupt */
00056             else
00057             {
00058                 /* WaitForSingleObjectEx() uses milliseconds, round up */
00059                 waittime = (timerCommArea.value.it_value.tv_usec + 999) / 1000 +
00060                     timerCommArea.value.it_value.tv_sec * 1000;
00061             }
00062             ResetEvent(timerCommArea.event);
00063             LeaveCriticalSection(&timerCommArea.crit_sec);
00064         }
00065         else if (r == WAIT_TIMEOUT)
00066         {
00067             /* Timeout expired, signal SIGALRM and turn it off */
00068             pg_queue_signal(SIGALRM);
00069             waittime = INFINITE;
00070         }
00071         else
00072         {
00073             /* Should never happen */
00074             Assert(false);
00075         }
00076     }
00077 
00078     return 0;
00079 }
00080 
00081 /*
00082  * Win32 setitimer emulation by creating a persistent thread
00083  * to handle the timer setting and notification upon timeout.
00084  */
00085 int
00086 setitimer(int which, const struct itimerval * value, struct itimerval * ovalue)
00087 {
00088     Assert(value != NULL);
00089     Assert(value->it_interval.tv_sec == 0 && value->it_interval.tv_usec == 0);
00090     Assert(which == ITIMER_REAL);
00091 
00092     if (timerThreadHandle == INVALID_HANDLE_VALUE)
00093     {
00094         /* First call in this backend, create event and the timer thread */
00095         timerCommArea.event = CreateEvent(NULL, TRUE, FALSE, NULL);
00096         if (timerCommArea.event == NULL)
00097             ereport(FATAL,
00098              (errmsg_internal("could not create timer event: error code %lu",
00099                               GetLastError())));
00100 
00101         MemSet(&timerCommArea.value, 0, sizeof(struct itimerval));
00102 
00103         InitializeCriticalSection(&timerCommArea.crit_sec);
00104 
00105         timerThreadHandle = CreateThread(NULL, 0, pg_timer_thread, NULL, 0, NULL);
00106         if (timerThreadHandle == INVALID_HANDLE_VALUE)
00107             ereport(FATAL,
00108             (errmsg_internal("could not create timer thread: error code %lu",
00109                              GetLastError())));
00110     }
00111 
00112     /* Request the timer thread to change settings */
00113     EnterCriticalSection(&timerCommArea.crit_sec);
00114     if (ovalue)
00115         *ovalue = timerCommArea.value;
00116     timerCommArea.value = *value;
00117     LeaveCriticalSection(&timerCommArea.crit_sec);
00118     SetEvent(timerCommArea.event);
00119 
00120     return 0;
00121 }