Set up current working directory on File->Open
[gscope.git] / backend-proto.c
blobcaab6274d4e412991bb61f88b560f15999db7019
1 /*
2 * (c) 2010 Cyrill Gorcunov, gorcunov@gmail.com
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or (at
7 * your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
18 * backend protocol
21 #include <unistd.h>
22 #include <stdio.h>
23 #include <stddef.h>
24 #include <stdlib.h>
25 #include <stdarg.h>
26 #include <string.h>
27 #include <errno.h>
28 #include <limits.h>
29 #include <sys/param.h>
30 #include <sys/types.h>
32 #include <pthread.h>
34 #include "backend-proto.h"
35 #include "util.h"
37 static pthread_rwlock_t rwlock = PTHREAD_RWLOCK_INITIALIZER;
38 static struct backend_proto protocols[BACKEND_PROTO_MAX];
40 struct backend_proto *backend_proto_register(struct backend_proto *proto)
42 struct backend_proto *p = NULL;
43 unsigned int i, j = -1U;
45 pthread_rwlock_rdlock(&rwlock);
47 for (i = 0; i < ARRAY_SIZE(protocols); i++) {
48 if (protocols[i].protocol == BACKEND_PROTO_NONE) {
49 if (j == -1U)
50 j = i;
51 } else if (protocols[i].protocol == proto->protocol) {
52 p = &protocols[i];
53 goto unlock;
56 if (j != -1U) {
57 protocols[j] = *proto;
58 p = &protocols[i];
59 goto unlock;
62 unlock:
63 pthread_rwlock_unlock(&rwlock);
64 return p;
67 void backend_proto_unregister(struct backend_proto *proto)
69 unsigned int i;
71 pthread_rwlock_wrlock(&rwlock);
73 for (i = 0; i < ARRAY_SIZE(protocols); i++) {
74 if (protocols[i].protocol == proto->protocol) {
75 protocols[i].protocol = BACKEND_PROTO_NONE;
76 break;
80 pthread_rwlock_unlock(&rwlock);
83 struct backend_proto *backend_proto_find(enum backend_protocols proto)
85 struct backend_proto *p = NULL;
86 unsigned int i;
88 pthread_rwlock_rdlock(&rwlock);
90 for (i = 0; i < ARRAY_SIZE(protocols); i++) {
91 if (protocols[i].protocol == proto) {
92 p = &protocols[i];
93 break;
97 pthread_rwlock_unlock(&rwlock);
98 return p;