Main Page   Class Hierarchy   Alphabetical List   Data Structures   File List   Data Fields   Globals  

win32_sem.c

Go to the documentation of this file.
00001 // Semaphore functions for WIN32  (Windows NT/95/98 and CE)
00002 
00003 #include "rcs_defs.hh"
00004 
00005 #if defined(WIN32) && !defined(USE_OLD_WINSOCK)
00006 // Lame problem if windows.h is included before winsock2.h many redefined
00007 // compiler errors result.
00008 #include <winsock2.h>
00009 #endif
00010 
00011 #include <windows.h>
00012 #include <winbase.h>
00013 
00014 #ifndef NO_STDIO
00015 #include <stdio.h>              /* sprintf() */
00016 #endif
00017 
00018 #include "dbg_mem.h"
00019 
00020 #ifdef UNDER_CE
00021 #include "rcs_ce.h"
00022 #endif
00023 
00024 int
00025 rcs_sem_clear (rcs_sem_t * sem)
00026 {
00027   /* Un implimented. */
00028   return (0);
00029 }
00030 
00031 rcs_sem_t *
00032 rcs_sem_open (const char *name, int oflag, /*  mode ... */ ...)
00033 {
00034 #if defined(WIN32) && defined(UNICODE)
00035   wchar_t wname[64];
00036 #endif
00037   unsigned long int id = (unsigned long int) name;
00038   rcs_sem_t *sem;
00039   sem = (rcs_sem_t *) DEBUG_MALLOC (sizeof (rcs_sem_t));
00040   if (NULL == sem)
00041     {
00042       rcs_print_error ("Out of memory. (Error = %d\n)", GetLastError ());
00043       return NULL;
00044     }
00045 
00046 #ifndef NO_STDIO
00047   sprintf (sem->name, "sem%d", (int) id);
00048 #else
00049   strcpy (sem->name, "sem");
00050   _itoa (id, sem->name + 3, 10);
00051 #endif
00052 
00053 #ifdef UNDER_CE
00054 #ifdef UNICODE
00055   RCS_CE_ASCII_TO_UNICODE (wname, sem->name, 64);
00056   sem->handle = CreateMutex (NULL, FALSE, wname);
00057 #else
00058   sem->handle = CreateMutex (NULL, FALSE, sem->name);
00059 #endif
00060   if (NULL == sem->handle)
00061     {
00062       rcs_print_error ("Can not create semaphore. (Error = %d)\n",
00063                        GetLastError ());
00064       free (sem);
00065       return NULL;
00066     }
00067 #else
00068   sem->handle = OpenMutex (MUTEX_ALL_ACCESS, TRUE, sem->name);
00069   if (NULL == sem->handle)
00070     {
00071       rcs_print_error ("Can not create semaphore. (Error = %d)\n",
00072                        GetLastError ());
00073       free (sem);
00074       return NULL;
00075     }
00076 #endif
00077   return sem;
00078 }
00079 
00080 rcs_sem_t *
00081 rcs_sem_create (unsigned long int id, int mode, int state)
00082 {
00083 #ifdef UNICODE
00084   wchar_t wname[64];
00085 #endif
00086   rcs_sem_t *sem;
00087 #ifndef UNDER_CE
00088   SECURITY_ATTRIBUTES sa;
00089   SECURITY_DESCRIPTOR sd;
00090   if (FALSE ==
00091       InitializeSecurityDescriptor (&sd, SECURITY_DESCRIPTOR_REVISION))
00092     {
00093       rcs_print_error
00094         ("Can not initailize security descriptor.(Error = %d)\n",
00095          GetLastError ());
00096       return NULL;
00097     }
00098   sa.nLength = sizeof (SECURITY_ATTRIBUTES);
00099   sa.lpSecurityDescriptor = &sd;
00100   sa.bInheritHandle = TRUE;
00101 #endif
00102   sem = (rcs_sem_t *) DEBUG_MALLOC (sizeof (rcs_sem_t));
00103   if (NULL == sem)
00104     {
00105       rcs_print_error ("Out of memory. (Error = %d\n)", GetLastError ());
00106       return NULL;
00107     }
00108 #ifndef NO_STDIO
00109   sprintf (sem->name, "sem%d", (int) id);
00110 #else
00111   strcpy (sem->name, "sem");
00112   _itoa (id, sem->name + 3, 10);
00113 #endif
00114 #ifdef UNDER_CE
00115 #ifdef UNICODE
00116   RCS_CE_ASCII_TO_UNICODE (wname, sem->name, 64);
00117   sem->handle = CreateMutex (NULL, FALSE, wname);
00118 #else
00119   sem->handle = CreateMutex (NULL, FALSE, sem->name);
00120 #endif
00121 #else
00122   sem->handle = CreateMutex (&sa, FALSE, sem->name);
00123 #endif
00124 
00125   if (NULL == sem->handle)
00126     {
00127       rcs_print_error ("Can not create semaphore. (Error = %d)\n",
00128                        GetLastError ());
00129       free (sem);
00130       return NULL;
00131     }
00132   return sem;
00133 }
00134 
00135 int
00136 rcs_sem_init (rcs_sem_t * sem, int pshared, unsigned int value)
00137 {
00138   return 0;
00139 }
00140 
00141 int
00142 rcs_sem_destroy (rcs_sem_t * sem)
00143 {
00144   return rcs_sem_close (sem);
00145 }
00146 
00147 int
00148 rcs_sem_close (rcs_sem_t * sem)
00149 {
00150   if (NULL != sem)
00151     {
00152       if (NULL != sem->handle)
00153         {
00154           CloseHandle (sem->handle);
00155           sem->handle = NULL;
00156         }
00157     }
00158   return 0;
00159 }
00160 
00161 int
00162 rcs_sem_unlink (const char *name)
00163 {
00164   rcs_sem_t *sem = (rcs_sem_t *) name;
00165   return rcs_sem_close (sem);
00166 }
00167 
00168 int
00169 rcs_sem_wait (rcs_sem_t * sem, double timeout)
00170 {
00171   int error;
00172   if (sem == NULL)
00173     {
00174       return -1;
00175     }
00176   if (sem->handle == NULL)
00177     {
00178       return -1;
00179     }
00180   if (WAIT_FAILED == WaitForSingleObject (sem->handle,
00181                                           ((unsigned long) (timeout * 1000))))
00182     {
00183       if (WAIT_TIMEOUT == (error = GetLastError ()))
00184         {
00185           return -2;
00186         }
00187       rcs_print_error ("WaitForSingleObject failed. (Error = %d)\n", error);
00188       return -1;
00189     }
00190   return 0;
00191 }
00192 
00193 
00194 int
00195 rcs_sem_trywait (rcs_sem_t * sem)
00196 {
00197   return rcs_sem_wait (sem, 0);
00198 }
00199 
00200 int
00201 rcs_sem_post (rcs_sem_t * sem)
00202 {
00203   if (FALSE == ReleaseMutex (sem->handle))
00204     {
00205       rcs_print_error ("ReleaseMutex failed. (Error = %d)\n",
00206                        GetLastError ());
00207       return -1;
00208     }
00209   return 0;
00210 }
00211 
00212 int
00213 rcs_sem_flush (rcs_sem_t * sem)
00214 {
00215   if (FALSE == ReleaseMutex (sem->handle))
00216     {
00217       rcs_print_error ("ReleaseMutex failed. (Error = %d)\n",
00218                        GetLastError ());
00219       return -1;
00220     }
00221   return 0;
00222 }
00223 
00224 
00225 int
00226 rcs_sem_getvalue (rcs_sem_t * sem, unsigned int *sval)
00227 {
00228   if (WAIT_FAILED == WaitForSingleObject (sem->handle, 0))
00229     {
00230       return 1;
00231     }
00232   return 0;
00233 }

Generated on Sun Dec 2 15:56:53 2001 for rcslib by doxygen1.2.11.1 written by Dimitri van Heesch, © 1997-2001