root/src/libsir/src/sirmutex.c

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

DEFINITIONS

This source file includes following definitions.
  1. _sir_mutexcreate
  2. _sir_mutexlock
  3. _sir_mutextrylock
  4. _sir_mutexunlock
  5. _sir_mutexdestroy
  6. _sir_mutexcreate
  7. _sir_mutexlock
  8. _sir_mutextrylock
  9. _sir_mutexunlock
  10. _sir_mutexdestroy

   1 /*
   2  * sirmutex.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/mutex.h"
  33 #include "sir/internal.h"
  34 #include "sir/platform.h"
  35 
  36 #if !defined(__WIN__) /* pthread implementation */
  37 bool _sir_mutexcreate(sir_mutex* mutex) {
     /* [previous][next][first][last][top][bottom][index][help] */
  38     if (_sir_validptr(mutex)) {
  39         pthread_mutexattr_t attr;
  40         int op = pthread_mutexattr_init(&attr);
  41         if (0 == op) {
  42             op = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
  43             if (0 == op) {
  44                 op = pthread_mutex_init(mutex, &attr);
  45                 if (0 == op)
  46                     return true;
  47             }
  48         }
  49 
  50         (void)_sir_handleerr(op);
  51     }
  52 
  53     return false;
  54 }
  55 
  56 bool _sir_mutexlock(sir_mutex* mutex) {
     /* [previous][next][first][last][top][bottom][index][help] */
  57     if (_sir_validptr(mutex)) {
  58         int op = pthread_mutex_lock(mutex);
  59         return 0 == op ? true : _sir_handleerr(op);
  60     }
  61 
  62     return false;
  63 }
  64 
  65 bool _sir_mutextrylock(sir_mutex* mutex) {
     /* [previous][next][first][last][top][bottom][index][help] */
  66     if (_sir_validptr(mutex)) {
  67         int op = pthread_mutex_trylock(mutex);
  68         return 0 == op ? true : _sir_handleerr(op);
  69     }
  70 
  71     return false;
  72 }
  73 
  74 bool _sir_mutexunlock(sir_mutex* mutex) {
     /* [previous][next][first][last][top][bottom][index][help] */
  75     if (_sir_validptr(mutex)) {
  76         int op = pthread_mutex_unlock(mutex);
  77         return 0 == op ? true : _sir_handleerr(op);
  78     }
  79 
  80     return false;
  81 }
  82 
  83 bool _sir_mutexdestroy(sir_mutex* mutex) {
     /* [previous][next][first][last][top][bottom][index][help] */
  84     if (_sir_validptr(mutex)) {
  85         int op = pthread_mutex_destroy(mutex);
  86         return 0 == op ? true : _sir_handleerr(op);
  87     }
  88 
  89     return false;
  90 }
  91 #else /* __WIN__ */
  92 bool _sir_mutexcreate(sir_mutex* mutex) {
     /* [previous][next][first][last][top][bottom][index][help] */
  93     if (_sir_validptr(mutex)) {
  94         InitializeCriticalSection(mutex);
  95         return true;
  96     }
  97 
  98     return false;
  99 }
 100 
 101 bool _sir_mutexlock(sir_mutex* mutex) {
     /* [previous][next][first][last][top][bottom][index][help] */
 102     if (_sir_validptr(mutex)) {
 103         EnterCriticalSection(mutex);
 104         return true;
 105     }
 106 
 107     return false;
 108 }
 109 
 110 bool _sir_mutextrylock(sir_mutex* mutex) {
     /* [previous][next][first][last][top][bottom][index][help] */
 111     if (_sir_validptr(mutex))
 112         return FALSE != TryEnterCriticalSection(mutex);
 113 
 114     return false;
 115 }
 116 
 117 bool _sir_mutexunlock(sir_mutex* mutex) {
     /* [previous][next][first][last][top][bottom][index][help] */
 118     if (_sir_validptr(mutex)) {
 119         LeaveCriticalSection(mutex);
 120         return true;
 121     }
 122 
 123     return false;
 124 }
 125 
 126 bool _sir_mutexdestroy(sir_mutex* mutex) {
     /* [previous][next][first][last][top][bottom][index][help] */
 127     if (_sir_validptr(mutex)) {
 128         DeleteCriticalSection(mutex);
 129         return true;
 130     }
 131 
 132     return false;
 133 }
 134 #endif /* !__WIN__ */

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