Header And Logo

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

Data Structures | Typedefs | Functions | Variables

timer.c File Reference

#include "postgres.h"
Include dependency graph for timer.c:

Go to the source code of this file.

Data Structures

struct  timerCA

Typedefs

typedef struct timerCA timerCA

Functions

static DWORD WINAPI pg_timer_thread (LPVOID param)
int setitimer (int which, const struct itimerval *value, struct itimerval *ovalue)

Variables

static timerCA timerCommArea
static HANDLE timerThreadHandle = INVALID_HANDLE_VALUE

Typedef Documentation

typedef struct timerCA timerCA

Function Documentation

static DWORD WINAPI pg_timer_thread ( LPVOID  param  )  [static]

Definition at line 36 of file timer.c.

References Assert, timerCA::crit_sec, timerCA::event, FALSE, itimerval::it_value, NULL, pg_queue_signal(), SIGALRM, timerCA::value, and waittime.

Referenced by setitimer().

{
    DWORD       waittime;

    Assert(param == NULL);

    waittime = INFINITE;

    for (;;)
    {
        int         r;

        r = WaitForSingleObjectEx(timerCommArea.event, waittime, FALSE);
        if (r == WAIT_OBJECT_0)
        {
            /* Event signalled from main thread, change the timer */
            EnterCriticalSection(&timerCommArea.crit_sec);
            if (timerCommArea.value.it_value.tv_sec == 0 &&
                timerCommArea.value.it_value.tv_usec == 0)
                waittime = INFINITE;    /* Cancel the interrupt */
            else
            {
                /* WaitForSingleObjectEx() uses milliseconds, round up */
                waittime = (timerCommArea.value.it_value.tv_usec + 999) / 1000 +
                    timerCommArea.value.it_value.tv_sec * 1000;
            }
            ResetEvent(timerCommArea.event);
            LeaveCriticalSection(&timerCommArea.crit_sec);
        }
        else if (r == WAIT_TIMEOUT)
        {
            /* Timeout expired, signal SIGALRM and turn it off */
            pg_queue_signal(SIGALRM);
            waittime = INFINITE;
        }
        else
        {
            /* Should never happen */
            Assert(false);
        }
    }

    return 0;
}

int setitimer ( int  which,
const struct itimerval value,
struct itimerval ovalue 
)

Definition at line 86 of file timer.c.

References Assert, timerCA::crit_sec, ereport, errmsg_internal(), timerCA::event, FALSE, FATAL, itimerval::it_interval, ITIMER_REAL, MemSet, NULL, pg_timer_thread(), timerThreadHandle, TRUE, and timerCA::value.

Referenced by disable_all_timeouts(), fork_process(), and schedule_alarm().

{
    Assert(value != NULL);
    Assert(value->it_interval.tv_sec == 0 && value->it_interval.tv_usec == 0);
    Assert(which == ITIMER_REAL);

    if (timerThreadHandle == INVALID_HANDLE_VALUE)
    {
        /* First call in this backend, create event and the timer thread */
        timerCommArea.event = CreateEvent(NULL, TRUE, FALSE, NULL);
        if (timerCommArea.event == NULL)
            ereport(FATAL,
             (errmsg_internal("could not create timer event: error code %lu",
                              GetLastError())));

        MemSet(&timerCommArea.value, 0, sizeof(struct itimerval));

        InitializeCriticalSection(&timerCommArea.crit_sec);

        timerThreadHandle = CreateThread(NULL, 0, pg_timer_thread, NULL, 0, NULL);
        if (timerThreadHandle == INVALID_HANDLE_VALUE)
            ereport(FATAL,
            (errmsg_internal("could not create timer thread: error code %lu",
                             GetLastError())));
    }

    /* Request the timer thread to change settings */
    EnterCriticalSection(&timerCommArea.crit_sec);
    if (ovalue)
        *ovalue = timerCommArea.value;
    timerCommArea.value = *value;
    LeaveCriticalSection(&timerCommArea.crit_sec);
    SetEvent(timerCommArea.event);

    return 0;
}


Variable Documentation

Definition at line 30 of file timer.c.

HANDLE timerThreadHandle = INVALID_HANDLE_VALUE [static]

Definition at line 31 of file timer.c.

Referenced by setitimer().