latch.h

text/plain

Filename: latch.h
Type: text/plain
Part: 1
Message: Latch implementation
/*-------------------------------------------------------------------------
 *
 * latch.h
 *       Routines for interprocess latches
 *
 *
 * Portions Copyright (c) 1996-2010, PostgreSQL Global Development Group
 * Portions Copyright (c) 1994, Regents of the University of California
 *
 * $PostgreSQL$
 *
 *-------------------------------------------------------------------------
 */

#ifndef _LATCH_H_
#define _LATCH_H_

#include <stdio.h>
#include <errno.h>
#include <signal.h>
#include <unistd.h>

/* Fake postgres. */

int MyProcPid;

#define false 0
#define true 1

typedef unsigned char bool;

typedef int pgsocket;

#define PGINVALID_SOCKET -1


#define elog(_x,...) printf(__VA_ARGS__)
#define Assert(_x) do { if ((_x) == 0) { printf("Assertion failure @%d\n", __LINE__); exit(1); }} while (0);
//#define DEBUG(...) printf(__VA_ARGS__) 
#define DEBUG(...) do { ; } while (0); 

union semun {
   int              val;    /* Value for SETVAL */
   struct semid_ds *buf;    /* Buffer for IPC_STAT, IPC_SET */
   unsigned short  *array;  /* Array for GETALL, SETALL */
   struct seminfo  *__buf;  /* Buffer for IPC_INFO
                               (Linux specific) */
}; 




/*
 * Latch structure should be treated as opaque and only accessed through
 * the public functions. It is defined here to allow embedding Latches as
 * part of bigger structs.
 */
typedef struct
{
#if defined(LINUX_SEM)
       int semId;
#elif defined(PIPE)
       int pipefds[2];
#else
       sig_atomic_t    is_set;
#endif
       bool                    is_shared;
#ifndef WIN32
       int                             owner_pid;
#else
       HANDLE                  event;
#endif
} Latch;

/*
 * prototypes for functions in latch.c
 */
extern void InitLatch(volatile Latch *latch);
extern void InitSharedLatch(volatile Latch *latch);
extern void OwnLatch(volatile Latch *latch);
extern void DisownLatch(volatile Latch *latch);
extern bool WaitLatch(volatile Latch *latch, long timeout);
extern int     WaitLatchOrSocket(volatile Latch *latch, pgsocket sock,
                                 long timeout);
extern void SetLatch(volatile Latch *latch);
extern void ResetLatch(volatile Latch *latch);
#define TestLatch(latch) (((volatile Latch *) latch)->is_set)

#ifndef LINUX_SEM
extern void latch_sigusr1_handler(void);
#endif

#endif