r956: README.BUILD - add more library recommendations
[cinelerra_cv/ct.git] / guicast / bcipc.C
blob5764591da9e6f4a4a18c5001247aba56a20d34c4
1 #include "bcipc.h"
2 #include "bcresources.h"
3 #include "bcsignals.h"
4 #include "sema.h"
5 #include <signal.h>
6 #include <stdlib.h>
7 #include <sys/sem.h>
8 #include <sys/msg.h>
9 #include <sys/shm.h>
15 static ArrayList<int> global_shmem_db;
16 static ArrayList<int> global_sema_db;
17 static ArrayList<int> global_msg_db;
18 static Mutex global_ipc_lock;
19 static int crashed = 0;
21 // These must be atomic routines
23 int bc_enter_id(ArrayList<int>* list, int id);
24 int bc_remove_id(ArrayList<int>* list, int id);
26 void bc_ipc_termination(int signum)
28         global_ipc_lock.lock();
31         if(!crashed)
32         {
34 #if 0
35                 union semun arg;
36                 int i;
38                 if(global_shmem_db.total || global_sema_db.total || global_msg_db.total)
39                         printf("Crash\n");
40                 for(i = 0; i < global_shmem_db.total; i++)
41                 {
42                         if(!shmctl(global_shmem_db.values[i], IPC_RMID, NULL))
43                         {
44                                 printf("Deleted shared memory %d\n", global_shmem_db.values[i]);
45                         }
46                 }
48                 for(i = 0; i < global_sema_db.total; i++)
49                 {
50                         if(!semctl(global_sema_db.values[i], 0, IPC_RMID, arg)) 
51                         {
52                                 printf("Deleted semaphore %d\n", global_sema_db.values[i]);
53                         }
54                 }
56                 for(i = 0; i < global_msg_db.total; i++)
57                 {
58                         if(!msgctl(global_msg_db.values[i], IPC_RMID, NULL)) 
59                         {
60                                 printf("Deleted message %d\n", global_msg_db.values[i]);
61                         }
62                 }
64                 global_shmem_db.remove_all();
65                 global_sema_db.remove_all();
66                 global_msg_db.remove_all();
70 #endif
73 // dispatch user specified signal handler
74                 if(BC_Resources::signal_handler) 
75                         BC_Resources::signal_handler->signal_handler(signum);
76                 crashed = 1;
77         }
78         global_ipc_lock.unlock();
79         exit(0);
82 int bc_init_ipc()
84         if(signal(SIGSEGV, bc_ipc_termination) == SIG_IGN)
85                 signal(SIGSEGV, SIG_IGN);
86         if(signal(SIGBUS, bc_ipc_termination) == SIG_IGN)
87                 signal(SIGBUS, SIG_IGN);
88         //      SIGKILL can't be ignored
89         //      if(signal(SIGKILL, bc_ipc_termination) == SIG_IGN)
90         //              signal(SIGKILL, SIG_IGN);
91         if(signal(SIGINT, bc_ipc_termination) == SIG_IGN)
92                 signal(SIGINT, SIG_IGN);
93         if(signal(SIGHUP, bc_ipc_termination) == SIG_IGN)
94                 signal(SIGHUP, SIG_IGN);
95         if(signal(SIGTERM, bc_ipc_termination) == SIG_IGN)
96                 signal(SIGTERM, SIG_IGN);
97         return 0;
101 int bc_enter_shmem_id(int id)
103         return bc_enter_id(&global_shmem_db, id);
106 int bc_remove_shmem_id(int id)
108         return bc_remove_id(&global_shmem_db, id);
111 int bc_enter_sema_id(int id)
113         return bc_enter_id(&global_sema_db, id);
116 int bc_remove_sema_id(int id)
118         return bc_remove_id(&global_sema_db, id);
121 int bc_enter_msg_id(int id)
123         return bc_enter_id(&global_msg_db, id);
126 int bc_remove_msg_id(int id)
128         return bc_remove_id(&global_msg_db, id);
131 int bc_enter_id(ArrayList<int>* list, int id)
133         int i, result = 0;
134         global_ipc_lock.lock();
135         for(i = 0; i < list->total; i++)
136         {       
137                 if(list->values[i] == id) result = 1;
138         }
139         if(!result) list->append(id);
140         global_ipc_lock.unlock();
141         return 0;
144 int bc_remove_id(ArrayList<int>* list, int id)
146         int i;
147         global_ipc_lock.lock();
148         for(i = 0; i < list->total; i++)
149         {
150                 if(list->values[i] == id) list->remove_number(i);
151         }
152         global_ipc_lock.unlock();
153         return 0;