root/src/libsir/src/sircondition.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. _sir_condcreate
  2. _sir_condsignal
  3. _sir_condbroadcast
  4. _sir_conddestroy
  5. _sir_condwait
  6. _sir_condwait_timeout

   1 /**
   2  * @file sircondition.c
   3  *
   4  * @version 2.2.5
   5  *
   6  * -----------------------------------------------------------------------------
   7  *
   8  * SPDX-License-Identifier: MIT
   9  *
  10  * Copyright (c) 2018-2024 Ryan M. Lederman <lederman@gmail.com>
  11  *
  12  * Permission is hereby granted, free of charge, to any person obtaining a copy of
  13  * this software and associated documentation files (the "Software"), to deal in
  14  * the Software without restriction, including without limitation the rights to
  15  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  16  * the Software, and to permit persons to whom the Software is furnished to do so,
  17  * subject to the following conditions:
  18  *
  19  * The above copyright notice and this permission notice shall be included in all
  20  * copies or substantial portions of the Software.
  21  *
  22  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  23  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  24  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  25  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  26  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  27  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28  *
  29  * -----------------------------------------------------------------------------
  30  */
  31 
  32 #include "sir/condition.h"
  33 #include "sir/internal.h"
  34 #include "sir/platform.h"
  35 
  36 bool _sir_condcreate(sir_condition* cond) {
     /* [previous][next][first][last][top][bottom][index][help] */
  37     bool valid = _sir_validptr(cond);
  38 
  39     if (valid) {
  40 #if !defined(__WIN__)
  41         int op = pthread_cond_init(cond, NULL);
  42         valid = 0 == op ? true : _sir_handleerr(op);
  43 #else
  44         InitializeConditionVariable(cond);
  45 #endif
  46     }
  47 
  48     return valid;
  49 }
  50 
  51 #if 0
  52 bool _sir_condsignal(sir_condition* cond) {
     /* [previous][next][first][last][top][bottom][index][help] */
  53     bool valid = _sir_validptr(cond);
  54 
  55     if (valid) {
  56 # if !defined(__WIN__)
  57         int op = pthread_cond_signal(cond);
  58         valid = 0 == op ? true : _sir_handleerr(op);
  59 # else
  60         WakeConditionVariable(cond);
  61 # endif
  62     }
  63 
  64     return valid;
  65 }
  66 #endif
  67 
  68 bool _sir_condbroadcast(sir_condition* cond) {
     /* [previous][next][first][last][top][bottom][index][help] */
  69     bool valid = _sir_validptr(cond);
  70 
  71     if (valid) {
  72 #if !defined(__WIN__)
  73         int op = pthread_cond_broadcast(cond);
  74         valid = 0 == op ? true : _sir_handleerr(op);
  75 #else
  76         WakeAllConditionVariable(cond);
  77 #endif
  78     }
  79 
  80     return valid;
  81 }
  82 
  83 bool _sir_conddestroy(sir_condition* cond) {
     /* [previous][next][first][last][top][bottom][index][help] */
  84     bool valid = _sir_validptr(cond);
  85 
  86     if (valid) {
  87 #if !defined(__WIN__)
  88         int op = pthread_cond_destroy(cond);
  89         valid = 0 == op ? true : _sir_handleerr(op);
  90 #else
  91         SIR_UNUSED(cond);
  92 #endif
  93     }
  94 
  95     return valid;
  96 }
  97 
  98 #if 0
  99 bool _sir_condwait(sir_condition* cond, sir_mutex* mutex) {
     /* [previous][next][first][last][top][bottom][index][help] */
 100     bool valid = _sir_validptr(cond) && _sir_validptr(mutex);
 101 
 102     if (valid) {
 103 # if !defined(__WIN__)
 104         int op = pthread_cond_wait(cond, mutex);
 105         valid = 0 == op ? true : _sir_handleerr(op);
 106 # else
 107         DWORD howlong = INFINITE;
 108         valid = _sir_condwait_timeout(cond, mutex, &howlong);
 109 # endif
 110     }
 111 
 112     return valid;
 113 }
 114 #endif
 115 
 116 bool _sir_condwait_timeout(sir_condition* cond, sir_mutex* mutex,
     /* [previous][next][first][last][top][bottom][index][help] */
 117     const sir_wait* howlong) {
 118     bool valid = _sir_validptr(cond) && _sir_validptr(mutex) && _sir_validptr(howlong);
 119 
 120     if (valid) {
 121 #if !defined(__WIN__)
 122         int op = pthread_cond_timedwait(cond, mutex, howlong);
 123         valid = 0 == op ? true : ETIMEDOUT == op ? false : _sir_handleerr(op);
 124 #else
 125         valid = (FALSE != SleepConditionVariableCS(cond, mutex, *howlong))
 126             ? true : _sir_handlewin32err(GetLastError());
 127 #endif
 128     }
 129 
 130     return valid;
 131 }

/* [previous][next][first][last][top][bottom][index][help] */