Commit c72d79dd1f4a93c0c7cd07821d8e6416eb48952f

Authored by henyxia
1 parent c21a2c16

Semaphores controlled

Showing 2 changed files with 98 additions and 8 deletions   Show diff stats
... ... @@ -6,16 +6,95 @@
6 6 #include <fcntl.h>
7 7 #include <errno.h>
8 8 #include <string.h>
  9 +#include <sys/ipc.h>
  10 +#include <sys/sem.h>
9 11 #include "printx.h"
10 12 #include "ui.h"
11 13 #include "nfc.h"
12 14 #include "hvc.h"
13 15  
14   -#define CMD_MAX 70
  16 +#define CMD_MAX 70
  17 +#define KEY 1100
  18 +#define SEM_NUMBER 2
  19 +#define SEM_INPUT 0
  20 +#define SEM_OUTPUT 1
15 21  
16 22 int bus;
17 23 bool busFree = true;
18 24 bool busStop = false;
  25 +int mySem;
  26 +
  27 +int get_sem(int i)
  28 +{
  29 + ushort semarr[30];
  30 + union semun
  31 + {
  32 + int val;
  33 + struct semid_ds *buf;
  34 + ushort *array;
  35 + }arg;
  36 +
  37 + arg.array = semarr;
  38 + semctl(mySem, i, GETALL, arg);
  39 + return semarr[i];
  40 +}
  41 +
  42 +void show_sem(int i)
  43 +{
  44 + int val;
  45 + val = get_sem(i);
  46 + printf("semaphore[%d]=%d\n", i, val);
  47 +}
  48 +
  49 +void create_sem(int N)
  50 +{
  51 + printx(DEBUG, BUS, "create %d semaphores\n", N);
  52 + mySem = semget(KEY, N, 0666 | IPC_CREAT);
  53 + if(mySem < 0)
  54 + printx(ERROR, BUS, "Unable to create the semaphore\n");
  55 +}
  56 +
  57 +void init_sem(int N)
  58 +{
  59 + int j;
  60 + int retval;
  61 + union semun
  62 + {
  63 + int val;
  64 + struct semid_ds *buf;
  65 + ushort *array;
  66 + }arg;
  67 + arg.val = 1;
  68 + for (j=0; j<N; j++)
  69 + {
  70 + retval = semctl(mySem, j, SETVAL, arg);
  71 + if(retval < 0)
  72 + printx(ERROR, BUS, "Unable to initialize the semaphore\n");
  73 + }
  74 +}
  75 +
  76 +void PV(int i, int act)
  77 +{
  78 + struct sembuf op;
  79 + int retval;
  80 +
  81 + op.sem_num = i;
  82 + op.sem_op = act; //1=V, -1=P
  83 + op.sem_flg = 0; //will wait
  84 + retval = semop(mySem, &op, 1);
  85 + if(retval != 0)
  86 + printx(ERROR, BUS, "Error semop will do %d\n", act);
  87 +}
  88 +
  89 +void P(int i)
  90 +{
  91 + PV(i, -1);
  92 +}
  93 +
  94 +void V(int i)
  95 +{
  96 + PV(i, 1);
  97 +}
19 98  
20 99 void stopBus()
21 100 {
... ... @@ -59,7 +138,7 @@ void processCmd(char* buffer)
59 138 setHeatTimer(5);
60 139 setHeatWantedState(true);
61 140 }
62   - else if(strcmp(buffer, "setheaton35s") == 0)
  141 + else if(strcmp(buffer, "setheaton10s") == 0)
63 142 {
64 143 printx(INFO, BUS, "Setting HEAT ON for 10 secs");
65 144 setHeatTimer(10);
... ... @@ -82,8 +161,9 @@ void* processBus(void* we)
82 161 printx(DEBUG, BUS, "Waiting for events\n");
83 162 while(!busStop)
84 163 {
85   - while(busFree);
86   - while(!busFree);
  164 + P(SEM_OUTPUT);
  165 + //while(busFree);
  166 + //while(!busFree);
87 167 printx(DEBUG, BUS, "Event receved !\n");
88 168 busFree = false;
89 169 lseek(bus, 0, SEEK_SET);
... ... @@ -93,12 +173,17 @@ void* processBus(void* we)
93 173 processCmd(buffer);
94 174 busFree = true;
95 175 printx(DEBUG, BUS, buffer);
  176 + V(SEM_INPUT);
96 177 }
97 178 return NULL;
98 179 }
99 180  
100 181 bool initBus()
101 182 {
  183 + create_sem(SEM_NUMBER);
  184 + printx(ERROR, BUS, "Oh dear, something went wrong with %s !\n", strerror(errno));
  185 + init_sem(SEM_INPUT);
  186 + init_sem(SEM_OUTPUT);
102 187 bus = creat("bus.log", 0666);
103 188 if(bus == -1)
104 189 {
... ... @@ -106,16 +191,18 @@ bool initBus()
106 191 return false;
107 192 }
108 193  
  194 + //P(SEM_INPUT);
  195 +
109 196 return true;
110 197 }
111 198  
112 199 void sendToBus(char* cmd)
113 200 {
114   - while(!busFree);
115   - busFree = false;
  201 + printx(DEBUG, BUS, "Control semaphore %d in %d out %d\n", mySem, get_sem(SEM_INPUT), get_sem(SEM_OUTPUT));
  202 + P(SEM_INPUT);
116 203 ftruncate(bus, 0);
117 204 ftruncate(bus, CMD_MAX);
118 205 lseek(bus, 0, SEEK_SET);
119 206 dprintf(bus, "%s\n", cmd);
120   - busFree = true;
  207 + V(SEM_OUTPUT);
121 208 }
... ...
... ... @@ -11,7 +11,10 @@
11 11 int main(void)
12 12 {
13 13 int ret;
14   - pthread_t tUI, tNFC, tBUS, tHVC;
  14 + pthread_t tUI;
  15 + pthread_t tNFC;
  16 + pthread_t tBUS;
  17 + pthread_t tHVC;
15 18 initUILog();
16 19 if(!initLog())
17 20 {
... ...