rsys

Basic data structures and low-level features
git clone git://git.meso-star.fr/rsys.git
Log | Files | Refs | README | LICENSE

test_mutex.c (11517B)


      1 /* Copyright (C) 2013-2023, 2025 Vincent Forest (vaplv@free.fr)
      2  *
      3  * The RSys library is free software: you can redistribute it and/or modify
      4  * it under the terms of the GNU General Public License as published
      5  * by the Free Software Foundation, either version 3 of the License, or
      6  * (at your option) any later version.
      7  *
      8  * The RSys library is distributed in the hope that it will be useful,
      9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
     11  * GNU General Public License for more details.
     12  *
     13  * You should have received a copy of the GNU General Public License
     14  * along with the RSys library. If not, see <http://www.gnu.org/licenses/>. */
     15 
     16 #define _POSIX_C_SOURCE 199506L /* POSIX threads and nanosleep */
     17 
     18 #include "clock_time.h"
     19 #include "mutex.h"
     20 
     21 #include <string.h>
     22 #include <pthread.h>
     23 
     24 static const char src_str[] = {
     25   'R','c','v','f','b','q','r',' ','1',',',' ','X','A','R','R',
     26   '-','Q','R','R','C',' ','V','A',' ','G','U','R',' ','Q','R',
     27   'N','Q',':','\n','-','-','-','-','-','-','-','-','-','-','-',
     28   '-','-','-','-','-','-','-','-','-','-','-','-','-','-','-',
     29   '-','-','-','-','-','-','-','-','\n','\n','B','A','P','R',' ',
     30   'L','B','H',' ','O','R','N','G',' ','G','U','R',' ','O','V',
     31   'T',' ','O','N','Q','N','F','F','R','F',' ','N','A','Q',' ',
     32   'P','Y','R','N','A',' ','B','H','G',' ','G','U','R',' ','Z',
     33   'B','B','A',' ','O','N','F','R',' ','L','B','H','\'','E','R',
     34   ' ','F','H','C','C','B','F','R','Q',' ','G','B','\n','J','V',
     35   'A',',',' ','N','E','R','A','\'','G',' ','L','B','H','?',' ',
     36   'N','E','R','A','\'','G',' ','L','B','H','?',' ','J','U','R',
     37   'E','R','\'','F',' ','L','B','H','E',' ','S','N','G',' ','E',
     38   'R','J','N','E','Q',' ','N','A','Q',' ','G','V','P','X','R',
     39   'G',' ','U','B','Z','R','?',' ','J','U','N','G','\n','G','U',
     40   'R',' ','U','R','Y','Y',' ','V','F',' ','G','U','V','F','?',
     41   ' ','V','G','\'','F',' ','A','B','G',' ','F','H','C','C','B',
     42   'F','R','Q',' ','G','B',' ','R','A','Q',' ','G','U','V','F',
     43   ' ','J','N','L','!','\n','\n','V','G',' ','F','G','V','A','X',
     44   'F',' ','Y','V','X','R',' ','E','B','G','G','R','A',' ','Z',
     45   'R','N','G',',',' ','O','H','G',' ','Y','B','B','X','F',' ',
     46   'Y','V','X','R',' ','G','U','R',' ','Y','B','F','G',' ','Q',
     47   'R','V','Z','B','F',' ','O','N','F','R','.',' ','Y','B','B',
     48   'X','F',' ','Y','V','X','R','\n','L','B','H','\'','E','R',' ',
     49   'F','G','H','P','X',' ','B','A',' ','G','U','R',' ','F','U',
     50   'B','E','R','F',' ','B','S',' ','U','R','Y','Y','.',' ','G',
     51   'U','R',' ','B','A','Y','L',' ','J','N','L',' ','B','H','G',
     52   ' ','V','F',' ','G','U','E','B','H','T','U','.','\n','\n','G',
     53   'B',' ','P','B','A','G','V','A','H','R',' ','G','U','R',' ',
     54   'Q','B','B','Z',' ','R','K','C','R','E','V','R','A','P','R',
     55   ',',' ','C','Y','N','L',' ','G','U','R',' ','F','U','B','E',
     56   'R','F',' ','B','S',' ','U','R','Y','Y',' ','N','A','Q',' ',
     57   'V','G','F',' ','N','Z','N','M','V','A','T','\n','F','R','D',
     58   'H','R','Y',',',' ','V','A','S','R','E','A','B','!','\n','\n',
     59   'R','c','v','f','b','q','r',' ','2',',',' ','G','U','R',' ',
     60   'F','U','B','E','R','F',' ','B','S',' ','U','R','Y','Y',':',
     61   '\n','-','-','-','-','-','-','-','-','-','-','-','-','-','-',
     62   '-','-','-','-','-','-','-','-','-','-','-','-','-','-','-',
     63   '-','\n','\n','L','B','H','\'','I','R',' ','Q','B','A','R',' ',
     64   'V','G','!',' ','G','U','R',' ','U','V','Q','R','B','H','F',
     65   ' ','P','L','O','R','E','-',' ','Q','R','Z','B','A',' ','Y',
     66   'B','E','Q',' ','G','U','N','G',' ','E','H','Y','R','Q',' ',
     67   'G','U','R',' ','Y','B','F','G',' ','Q','R','V','Z','B','F',
     68   ' ','Z','B','B','A','\n','O','N','F','R',' ','U','N','F',' ',
     69   'O','R','R','A',' ','F','Y','N','V','A',' ','N','A','Q',' ',
     70   'L','B','H',' ','N','E','R',' ','G','E','V','H','Z','C','U',
     71   'N','A','G','!',' ','O','H','G',' ','.','.','.',' ','J','U',
     72   'R','E','R',' ','N','E','R',' ','L','B','H','?',' ','L','B',
     73   'H','\n','P','Y','N','Z','O','R','E',' ','G','B',' ','G','U',
     74   'R',' ','R','Q','T','R',' ','B','S',' ','G','U','R',' ','Z',
     75   'B','B','A',' ','N','A','Q',' ','Y','B','B','X',' ','Q','B',
     76   'J','A',' ','G','B',' ','F','R','R',' ','G','U','R',' ','N',
     77   'J','S','H','Y',' ','G','E','H','G','U','.','\n','\n','Q','R',
     78   'V','Z','B','F',' ','S','Y','B','N','G','F',' ','N','O','B',
     79   'I','R',' ','U','R','Y','Y',' ','V','G','F','R','Y','S','!',
     80   ' ',' ','L','B','H','\'','I','R',' ','A','R','I','R','E',' ',
     81   'U','R','N','E','Q',' ','B','S',' ','N','A','L','B','A','R',
     82   ' ','R','F','P','N','C','V','A','T',' ','S','E','B','Z','\n',
     83   'U','R','Y','Y',',',' ','O','H','G',' ','L','B','H','\'','Y',
     84   'Y',' ','Z','N','X','R',' ','G','U','R',' ','O','N','F','G',
     85   'N','E','Q','F',' ','F','B','E','E','L',' ','G','U','R','L',
     86   ' ','R','I','R','E',' ','U','R','N','E','Q',' ','B','S',' ',
     87   'L','B','H','!',' ','D','H','V','P','X','Y','L',',',' ','L',
     88   'B','H','\n','E','N','C','C','R','Y',' ','Q','B','J','A',' ',
     89   'G','B',' ','G','U','R',' ','F','H','E','S','N','P','R',' ',
     90   'B','S',' ','U','R','Y','Y','.','\n','\n','A','B','J',',',' ',
     91   'V','G','\'','F',' ','B','A',' ','G','B',' ','G','U','R',' ',
     92   'S','V','A','N','Y',' ','P','U','N','C','G','R','E',' ','B',
     93   'S',' ','Q','B','B','Z','!',' ','-','-',' ','V','A','S','R',
     94   'E','A','B','.','\n','\n','R','c','v','f','b','q','r',' ','3',
     95   ',',' ','V','A','S','R','E','A','B',':','\n','-','-','-','-',
     96   '-','-','-','-','-','-','-','-','-','-','-','-','-','-','-',
     97   '\n','\n','G','U','R',' ','Y','B','N','G','U','F','B','Z','R',
     98   ' ','F','C','V','Q','R','E','Q','R','Z','B','A',' ','G','U',
     99   'N','G',' ','Z','N','F','G','R','E','Z','V','A','Q','R','Q',
    100   ' ','G','U','R',' ','V','A','I','N','F','V','B','A',' ','B',
    101   'S',' ','G','U','R',' ','Z','B','B','A',' ','O','N','F','R',
    102   'F','\n','N','A','Q',' ','P','N','H','F','R','Q',' ','F','B',
    103   ' ','Z','H','P','U',' ','Q','R','N','G','U',' ','U','N','F',
    104   ' ','U','N','Q',' ','V','G','F',' ','N','F','F',' ','X','V',
    105   'P','X','R','Q',' ','S','B','E',' ','N','Y','Y',' ','G','V',
    106   'Z','R','.','\n','\n','N',' ','U','V','Q','Q','R','A',' ','Q',
    107   'B','B','E','J','N','L',' ','B','C','R','A','F',' ','N','A',
    108   'Q',' ','L','B','H',' ','R','A','G','R','E','.',' ',' ','L',
    109   'B','H','\'','I','R',' ','C','E','B','I','R','A',' ','G','B',
    110   'B',' ','G','B','H','T','U',' ','S','B','E',' ','U','R','Y',
    111   'Y',' ','G','B','\n','P','B','A','G','N','V','A',',',' ','N',
    112   'A','Q',' ','A','B','J',' ','U','R','Y','Y',' ','N','G',' ',
    113   'Y','N','F','G',' ','C','Y','N','L','F',' ','S','N','V','E',
    114   ' ','-','-',' ','S','B','E',' ','L','B','H',' ','R','Z','R',
    115   'E','T','R',' ','S','E','B','Z',' ','G','U','R',' ','Q','B',
    116   'B','E',' ','G','B','\n','F','R','R',' ','G','U','R',' ','T',
    117   'E','R','R','A',' ','S','V','R','Y','Q','F',' ','B','S',' ',
    118   'R','N','E','G','U','!',' ',' ','U','B','Z','R',' ','N','G',
    119   ' ','Y','N','F','G','.','\n','\n','L','B','H',' ','J','B','A',
    120   'Q','R','E',' ','J','U','N','G','\'','F',' ','O','R','R','A',
    121   ' ','U','N','C','C','R','A','V','A','T',' ','B','A',' ','R',
    122   'N','E','G','U',' ','J','U','V','Y','R',' ','L','B','H',' ',
    123   'J','R','E','R',' ','O','N','G','G','Y','V','A','T',' ','R',
    124   'I','V','Y','\n','H','A','Y','R','N','F','U','R','Q','.',' ',
    125   'V','G','\'','F',' ','T','B','B','Q',' ','G','U','N','G',' ',
    126   'A','B',' ','U','R','Y','Y','-',' ','F','C','N','J','A',' ',
    127   'P','B','H','Y','Q',' ','U','N','I','R',' ','P','B','Z','R',
    128   ' ','G','U','E','B','H','T','U',' ','G','U','N','G',' ','Q',
    129   'B','B','E','\n','J','V','G','U',' ','L','B','H',' ','.','.',
    130   '.','\n','\n','R','c','v','f','b','q','r',' ','4',',',' ','G',
    131   'U','L',' ','S','Y','R','F','U',' ','P','B','A','F','H','Z',
    132   'R','Q',':','\n','-','-','-','-','-','-','-','-','-','-','-',
    133   '-','-','-','-','-','-','-','-','-','-','-','-','-','-','-',
    134   '-','-','-','-','\n','\n','G','U','R',' ','F','C','V','Q','R',
    135   'E',' ','Z','N','F','G','R','E','Z','V','A','Q',' ','Z','H',
    136   'F','G',' ','U','N','I','R',' ','F','R','A','G',' ','S','B',
    137   'E','G','U',' ','V','G','F',' ','Y','R','T','V','B','A','F',
    138   ' ','B','S',' ','U','R','Y','Y','F','C','N','J','A',' ','O',
    139   'R','S','B','E','R','\n','L','B','H','E',' ','S','V','A','N',
    140   'Y',' ','P','B','A','S','E','B','A','G','N','G','V','B','A',
    141   ' ','J','V','G','U',' ','G','U','N','G',' ','G','R','E','E',
    142   'V','O','Y','R',' ','O','R','N','F','G',' ','S','E','B','Z',
    143   ' ','U','R','Y','Y','.',' ','O','H','G',' ','L','B','H',' ',
    144   'F','G','R','C','C','R','Q','\n','S','B','E','J','N','E','Q',
    145   ' ','N','A','Q',' ','O','E','B','H','T','U','G',' ','S','B',
    146   'E','G','U',' ','R','G','R','E','A','N','Y',' ','Q','N','Z',
    147   'A','N','G','V','B','A',' ','N','A','Q',' ','F','H','S','S',
    148   'R','E','V','A','T',' ','H','C','B','A',' ','G','U','R',' ',
    149   'U','B','E','Q','R',' ','N','F',' ','N','\n','G','E','H','R',
    150   ' ','U','R','E','B',' ','J','B','H','Y','Q',' ','V','A',' ',
    151   'G','U','R',' ','S','N','P','R',' ','B','S',' ','F','B','Z',
    152   'R','G','U','V','A','T',' ','F','B',' ','R','I','V','Y','.',
    153   '\n','\n','O','R','F','V','Q','R','F',',',' ','F','B','Z','R',
    154   'B','A','R',' ','J','N','F',' ','T','B','A','A','N',' ','C',
    155   'N','L',' ','S','B','E',' ','J','U','N','G',' ','U','N','C',
    156   'C','R','A','R','Q',' ','G','B',' ','Q','N','V','F','L',',',
    157   ' ','L','B','H','E',' ','C','R','G',' ','E','N','O','O','V',
    158   'G','.','\n','\n','O','H','G',' ','A','B','J',',',' ','L','B',
    159   'H',' ','F','R','R',' ','F','C','E','R','N','Q',' ','O','R',
    160   'S','B','E','R',' ','L','B','H',' ','Z','B','E','R',' ','C',
    161   'B','G','R','A','G','V','N','Y',' ','C','N','V','A',' ','N',
    162   'A','Q',' ','T','V','O','O','V','G','H','Q','R',' ','N','F',
    163   ' ','N','\n','A','N','G','V','B','A',' ','B','S',' ','Q','R',
    164   'Z','B','A','F',' ','E','H','A',' ','N','Z','B','X',' ','V',
    165   'A',' ','B','H','E',' ','P','V','G','V','R','F','.','\n','\n',
    166   'A','R','K','G',' ','F','G','B','C',',',' ','U','R','Y','Y',
    167   ' ','B','A',' ','R','N','E','G','U','!','\n','\0'
    168 };
    169 
    170 struct string
    171 {
    172   struct mutex* mutex;
    173   char str[sizeof(src_str)/sizeof(char) + 1 /* +1 <=< '\0'*/ ];
    174   int i;
    175 };
    176 
    177 #include <unistd.h>
    178 
    179 static void*
    180 string_write(void* arg)
    181 {
    182   struct string* string = arg;
    183   struct timespec time;
    184   ASSERT(string);
    185 
    186   time.tv_sec = 0;
    187   time.tv_nsec = 10;
    188 
    189   for(;;) {
    190     int i = 0;
    191     mutex_lock(string->mutex);
    192 
    193     i = string->i;
    194 
    195     /* Forcing thread interleaving */
    196     CHK(nanosleep(&time, NULL) == 0);
    197 
    198     if((unsigned)i >= sizeof(src_str)/sizeof(char) + 1) {
    199       mutex_unlock(string->mutex);
    200       break;
    201     }
    202 
    203     string->str[string->i] = src_str[i];
    204     string->i = i+1;
    205 
    206     mutex_unlock(string->mutex);
    207   }
    208   return NULL;
    209 }
    210 
    211 static void
    212 test_mutex(void)
    213 {
    214   struct string string;
    215   struct time time_start, time_end, time_res;
    216   pthread_t thread;
    217   char dump[32];
    218 
    219   string.str[0] = '\0';
    220   string.i = 0;
    221 
    222   string.mutex = mutex_create();
    223   CHK(string.mutex != NULL);
    224 
    225   time_current(&time_start);
    226 
    227   /* Run a sub-thread */
    228   CHK(pthread_create(&thread, NULL, string_write, &string) == 0);
    229   string_write(&string);
    230 
    231   CHK(pthread_join(thread, NULL) == 0);
    232 
    233   time_current(&time_end);
    234   time_sub(&time_res, &time_end, &time_start);
    235 
    236   time_dump
    237     (&time_res,
    238      TIME_MSEC|TIME_USEC,
    239      NULL,
    240      dump,
    241      sizeof(dump)/sizeof(char));
    242   printf("%s\n", dump);
    243 
    244   CHK(string.i == sizeof(src_str)/sizeof(char) + 1);
    245   CHK(strcmp(string.str, src_str) == 0);
    246 
    247   mutex_destroy(string.mutex);
    248 }
    249 
    250 int
    251 main(int argc, char** argv)
    252 {
    253   (void)argc, (void)argv;
    254   test_mutex();
    255   return 0;
    256 }