Mercurial > dwindows
comparison os2/dw.c @ 557:1a210e2f214b
Added a bunch of support routines I now own due to the settlement
agreement with F/X. Shared memory and named event semaphores in
particular.
author | bsmith@81767d24-ef19-dc11-ae90-00e081727c95 |
---|---|
date | Sat, 17 Apr 2004 05:38:09 +0000 |
parents | 6707adaa093d |
children | 841445b0b457 |
comparison
equal
deleted
inserted
replaced
556:f4093dce8155 | 557:1a210e2f214b |
---|---|
7930 if(!eve || ~DosCloseEventSem(*eve)) | 7930 if(!eve || ~DosCloseEventSem(*eve)) |
7931 return FALSE; | 7931 return FALSE; |
7932 return TRUE; | 7932 return TRUE; |
7933 } | 7933 } |
7934 | 7934 |
7935 /* Create a named event semaphore which can be | |
7936 * opened from other processes. | |
7937 * Parameters: | |
7938 * eve: Pointer to an event handle to receive handle. | |
7939 * name: Name given to semaphore which can be opened | |
7940 * by other processes. | |
7941 */ | |
7942 HEV API dw_named_event_new(char *name) | |
7943 { | |
7944 int rc; | |
7945 char *semname = malloc(strlen(name)+8); | |
7946 HEV ev = 0; | |
7947 | |
7948 if(!semname) | |
7949 return 0; | |
7950 | |
7951 strcpy(semname, "\\sem32\\"); | |
7952 strcat(semname, name); | |
7953 | |
7954 DosCreateEventSem(semname, &ev, 0L, FALSE); | |
7955 | |
7956 free(semname); | |
7957 return ev; | |
7958 } | |
7959 | |
7960 /* Open an already existing named event semaphore. | |
7961 * Parameters: | |
7962 * eve: Pointer to an event handle to receive handle. | |
7963 * name: Name given to semaphore which can be opened | |
7964 * by other processes. | |
7965 */ | |
7966 HEV API dw_named_event_get(char *name) | |
7967 { | |
7968 char *semname = malloc(strlen(name)+8); | |
7969 HEV ev; | |
7970 | |
7971 if(!semname) | |
7972 return 0; | |
7973 | |
7974 strcpy(semname, "\\sem32\\"); | |
7975 strcat(semname, name); | |
7976 | |
7977 DosOpenEventSem(semname, &ev); | |
7978 | |
7979 free(semname); | |
7980 return ev; | |
7981 } | |
7982 | |
7983 /* Resets the event semaphore so threads who call wait | |
7984 * on this semaphore will block. | |
7985 * Parameters: | |
7986 * eve: Handle to the semaphore obtained by | |
7987 * an open or create call. | |
7988 */ | |
7989 int API dw_named_event_reset(HEV eve) | |
7990 { | |
7991 ULONG count; | |
7992 | |
7993 return DosResetEventSem(eve, &count); | |
7994 } | |
7995 | |
7996 /* Sets the posted state of an event semaphore, any threads | |
7997 * waiting on the semaphore will no longer block. | |
7998 * Parameters: | |
7999 * eve: Handle to the semaphore obtained by | |
8000 * an open or create call. | |
8001 */ | |
8002 int API dw_named_event_post(HEV eve) | |
8003 { | |
8004 return DosPostEventSem(eve); | |
8005 } | |
8006 | |
8007 | |
8008 /* Waits on the specified semaphore until it becomes | |
8009 * posted, or returns immediately if it already is posted. | |
8010 * Parameters: | |
8011 * eve: Handle to the semaphore obtained by | |
8012 * an open or create call. | |
8013 * timeout: Number of milliseconds before timing out | |
8014 * or -1 if indefinite. | |
8015 */ | |
8016 int API dw_named_event_wait(HEV eve, unsigned long timeout) | |
8017 { | |
8018 int rc; | |
8019 | |
8020 rc = DosWaitEventSem(eve, timeout); | |
8021 switch (rc) | |
8022 { | |
8023 case ERROR_INVALID_HANDLE: | |
8024 rc = DW_ERROR_NON_INIT; | |
8025 break; | |
8026 case ERROR_NOT_ENOUGH_MEMORY: | |
8027 rc = DW_ERROR_NO_MEM; | |
8028 break; | |
8029 case ERROR_INTERRUPT: | |
8030 rc = DW_ERROR_INTERRUPT; | |
8031 break; | |
8032 case ERROR_TIMEOUT: | |
8033 rc = DW_ERROR_TIMEOUT; | |
8034 break; | |
8035 } | |
8036 | |
8037 return rc; | |
8038 } | |
8039 | |
8040 /* Release this semaphore, if there are no more open | |
8041 * handles on this semaphore the semaphore will be destroyed. | |
8042 * Parameters: | |
8043 * eve: Handle to the semaphore obtained by | |
8044 * an open or create call. | |
8045 */ | |
8046 int API dw_named_event_close(HEV eve) | |
8047 { | |
8048 int rc; | |
8049 | |
8050 rc = DosCloseEventSem(eve); | |
8051 switch (rc) | |
8052 { | |
8053 case ERROR_INVALID_HANDLE: | |
8054 rc = DW_ERROR_NON_INIT; | |
8055 break; | |
8056 | |
8057 case ERROR_SEM_BUSY: | |
8058 rc = DW_ERROR_BUSY; | |
8059 break; | |
8060 } | |
8061 | |
8062 return rc; | |
8063 } | |
8064 | |
8065 /* | |
8066 * Allocates a shared memory region with a name. | |
8067 * Parameters: | |
8068 * handle: A pointer to receive a SHM identifier. | |
8069 * dest: A pointer to a pointer to receive the memory address. | |
8070 * size: Size in bytes of the shared memory region to allocate. | |
8071 * name: A string pointer to a unique memory name. | |
8072 */ | |
8073 int API dw_named_memory_alloc(HSHM *handle, void **dest, int size, char *name) | |
8074 { | |
8075 char namebuf[1024]; | |
8076 | |
8077 sprintf(namebuf, "\\sharemem\\%s", name); | |
8078 | |
8079 if(DosAllocSharedMem((void *)dest, namebuf, size, PAG_COMMIT | PAG_WRITE | PAG_READ) != NO_ERROR) | |
8080 return -1; | |
8081 | |
8082 return 0; | |
8083 } | |
8084 | |
8085 /* | |
8086 * Aquires shared memory region with a name. | |
8087 * Parameters: | |
8088 * dest: A pointer to a pointer to receive the memory address. | |
8089 * size: Size in bytes of the shared memory region to requested. | |
8090 * name: A string pointer to a unique memory name. | |
8091 */ | |
8092 int API dw_named_memory_get(HSHM *handle, void **dest, int size, char *name) | |
8093 { | |
8094 char namebuf[1024]; | |
8095 | |
8096 sprintf(namebuf, "\\sharemem\\%s", name); | |
8097 | |
8098 if(DosGetNamedSharedMem((void *)dest, namebuf, PAG_READ | PAG_WRITE) != NO_ERROR) | |
8099 return -1; | |
8100 | |
8101 return 0; | |
8102 } | |
8103 | |
8104 /* | |
8105 * Frees a shared memory region previously allocated. | |
8106 * Parameters: | |
8107 * handle: Handle obtained from DB_named_memory_allocate. | |
8108 * ptr: The memory address aquired with DB_named_memory_allocate. | |
8109 */ | |
8110 int API dw_named_memory_free(HSHM handle, void *ptr) | |
8111 { | |
8112 if(DosFreeMem(ptr) != NO_ERROR) | |
8113 return -1; | |
8114 return 0; | |
8115 } | |
8116 | |
7935 /* | 8117 /* |
7936 * Encapsulate the message queues on OS/2. | 8118 * Encapsulate the message queues on OS/2. |
7937 */ | 8119 */ |
7938 void _dwthreadstart(void *data) | 8120 void _dwthreadstart(void *data) |
7939 { | 8121 { |