| 1 |
/*************************************************************************\ |
| 2 |
* Copyright (c) 2021 Fritz Haber Institute, Berlin |
| 3 |
* SPDX-License-Identifier: EPICS |
| 4 |
* EPICS BASE is distributed subject to a Software License Agreement found |
| 5 |
* in file LICENSE that is included with this distribution. |
| 6 |
\*************************************************************************/ |
| 7 |
/* |
| 8 |
* RTEMS osdEvent.c |
| 9 |
* Author: H. Junkes |
| 10 |
* [email protected] |
| 11 |
*/ |
| 12 |
|
| 13 |
#include <stdio.h> |
| 14 |
#include <malloc.h> |
| 15 |
#include <stdint.h> |
| 16 |
#include <rtems.h> |
| 17 |
#include <rtems/error.h> |
| 18 |
|
| 19 |
#include <rtems/thread.h> |
| 20 |
|
| 21 |
#include "libComAPI.h" |
| 22 |
#include "epicsEvent.h" |
| 23 |
|
| 24 |
typedef struct epicsEventOSD { |
| 25 |
rtems_binary_semaphore rbs; |
| 26 |
} epicsEventOSD; |
| 27 |
|
| 28 |
/* |
| 29 |
* Create a simple binary semaphore |
| 30 |
*/ |
| 31 |
LIBCOM_API epicsEventId |
| 32 |
epicsEventCreate(epicsEventInitialState initialState) |
| 33 |
{ |
| 34 |
epicsEventOSD *pSem = malloc (sizeof(*pSem)); |
| 35 |
|
| 36 |
if (pSem) { |
| 37 |
rtems_binary_semaphore_init(&pSem->rbs, NULL); |
| 38 |
if (initialState) |
| 39 |
rtems_binary_semaphore_post(&pSem->rbs); |
| 40 |
} |
| 41 |
return pSem; |
| 42 |
} |
| 43 |
|
| 44 |
LIBCOM_API void |
| 45 |
epicsEventDestroy(epicsEventId pSem) |
| 46 |
{ |
| 47 |
rtems_binary_semaphore_destroy(&pSem->rbs); |
| 48 |
} |
| 49 |
|
| 50 |
LIBCOM_API epicsEventStatus |
| 51 |
epicsEventTrigger(epicsEventId pSem) |
| 52 |
{ |
| 53 |
rtems_binary_semaphore_post(&pSem->rbs); |
| 54 |
return epicsEventOK; |
| 55 |
} |
| 56 |
|
| 57 |
LIBCOM_API epicsEventStatus |
| 58 |
epicsEventWait(epicsEventId pSem) |
| 59 |
{ |
| 60 |
rtems_binary_semaphore_wait(&pSem->rbs); |
| 61 |
return epicsEventOK; |
| 62 |
} |
| 63 |
|
| 64 |
LIBCOM_API epicsEventStatus |
| 65 |
epicsEventWaitWithTimeout(epicsEventId pSem, double timeout) |
| 66 |
{ |
| 67 |
int sc; |
| 68 |
rtems_interval delay; |
| 69 |
rtems_interval rate = rtems_clock_get_ticks_per_second(); |
| 70 |
|
| 71 |
if (!rate) |
| 72 |
return epicsEventError; |
| 73 |
|
| 74 |
if (timeout <= 0.0) { |
| 75 |
sc = rtems_binary_semaphore_try_wait(&pSem->rbs); |
| 76 |
if (!sc) |
| 77 |
return epicsEventOK; |
| 78 |
else |
| 79 |
return epicsEventWaitTimeout; |
| 80 |
} |
| 81 |
else if (timeout < (double) UINT32_MAX / rate) { |
| 82 |
delay = timeout * rate; |
| 83 |
if (delay == 0) { |
| 84 |
/* 0 < timeout < 1/rate; round up */ |
| 85 |
delay = 1; |
| 86 |
} |
| 87 |
} |
| 88 |
else { |
| 89 |
/* timeout is NaN or too big to represent; wait forever */ |
| 90 |
delay = RTEMS_NO_TIMEOUT; |
| 91 |
} |
| 92 |
|
| 93 |
sc = rtems_binary_semaphore_wait_timed_ticks(&pSem->rbs, delay); |
| 94 |
if (!sc) |
| 95 |
return epicsEventOK; |
| 96 |
else if (sc == ETIMEDOUT) |
| 97 |
return epicsEventWaitTimeout; |
| 98 |
else |
| 99 |
return epicsEventError; |
| 100 |
} |
| 101 |
|
| 102 |
LIBCOM_API epicsEventStatus |
| 103 |
epicsEventTryWait(epicsEventId pSem) |
| 104 |
{ |
| 105 |
int sc = rtems_binary_semaphore_try_wait(&pSem->rbs); |
| 106 |
|
| 107 |
if (!sc) |
| 108 |
return epicsEventOK; |
| 109 |
else |
| 110 |
return epicsEventWaitTimeout; |
| 111 |
} |
| 112 |
|
| 113 |
LIBCOM_API void |
| 114 |
epicsEventShow(epicsEventId pSem, unsigned int level) |
| 115 |
{ |
| 116 |
} |