]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/dope/lib/dope/l4/init.c
Inital import
[l4.git] / l4 / pkg / dope / lib / dope / l4 / init.c
1 /*
2  * \brief   DOpE client library - L4 specific initialisation
3  * \date    2002-11-13
4  * \author  Norman Feske <nf2@inf.tu-dresden.de>
5  */
6
7 /*
8  * Copyright (C) 2002-2003  Norman Feske  <nf2@os.inf.tu-dresden.de>
9  * Technische Universitaet Dresden, Operating Systems Research Group
10  *
11  * This file is part of the DOpE package, which is distributed under
12  * the  terms  of the  GNU General Public Licence 2.  Please see the
13  * COPYING file for details.
14  */
15
16 /*** GENERAL INCLUDES ***/
17 #include <stdio.h>
18 #include <stdlib.h>
19
20 /*** L4 INCLUDES ***/
21 #include <l4/util/util.h>
22 #include <l4/sys/factory.h>
23 #include <l4/sys/icu.h>
24 #include <l4/re/c/namespace.h>
25 #include <l4/re/c/util/cap_alloc.h>
26 #include <l4/re/c/dataspace.h>
27 #include <l4/re/c/event.h>
28 #include <l4/re/c/event_buffer.h>
29 #include <l4/re/c/rm.h>
30
31 #include <pthread-l4.h>
32
33 /*** LOCAL INCLUDES ***/
34 #include "dopestd.h"
35 #include "dopelib.h"
36 #include "dopedef.h"
37 #include "sync.h"
38
39 l4_cap_idx_t dope_server;
40 l4re_ds_t ev_ds;
41 l4_cap_idx_t ev_irq;
42 static l4re_event_buffer_consumer_t ev_buf;
43
44 struct dopelib_mutex *dopelib_cmdf_mutex;
45 struct dopelib_mutex *dopelib_cmd_mutex;
46
47
48 void dopelib_usleep(int usec);
49 void dopelib_usleep(int usec) {
50         l4_sleep(usec/1000);
51 }
52
53 void dope_process_event(l4re_event_t *ev);
54
55 /*** INTERFACE: HANDLE EVENTLOOP OF A DOpE CLIENT ***/
56 void dope_eventloop(void)
57 {
58   l4re_event_buffer_consumer_process(&ev_buf, ev_irq,
59                                      pthread_getl4cap(pthread_self()),
60                                      dope_process_event);
61 }
62
63 l4re_event_t *dope_event_get(void)
64 {
65   return l4re_event_buffer_next(&ev_buf);
66 }
67
68 /*** INTERFACE: CONNECT TO DOpE SERVER AND INIT CLIENT LIB ***
69  *
70  * return 0 on success, != 0 otherwise
71  */
72 long dope_init(void)
73 {
74   static int initialized;
75   long ds_size;
76
77   /* avoid double initialization */
78   if (initialized)
79     return 0;
80
81   INFO(printf("DOpElib(dope_init): ask for 'dope'...\n"));
82
83   dope_server = l4re_get_env_cap("dope");
84   if (l4_is_invalid_cap(dope_server))
85     {
86       ERROR(printf("no 'dope' cap found!\n"));
87       return -L4_ENOENT;
88     }
89
90   INFO(printf("DOpElib(dope_init): found some DOpE.\n"));
91
92   ev_ds = l4re_util_cap_alloc();
93   ev_irq = l4re_util_cap_alloc();
94
95   if (l4_is_invalid_cap(ev_irq))
96     {
97       l4re_util_cap_free(ev_ds);
98       return -L4_ENOMEM;
99     }
100
101   if (l4_error(l4_factory_create_irq(l4re_env()->factory, ev_irq)))
102     {
103       l4re_util_cap_free(ev_ds);
104       l4re_util_cap_free(ev_irq);
105       return -L4_ENOMEM;
106     }
107
108   if (l4_error(l4_icu_bind(dope_server, 0, ev_irq)))
109     {
110       l4re_util_cap_free_um(dope_server);
111       l4re_util_cap_free(ev_ds);
112       l4re_util_cap_free_um(ev_irq);
113       return -L4_ENOMEM;
114     }
115
116   if (l4re_event_get(dope_server, ev_ds))
117     {
118       ERROR(printf("Did not get the event object!\n"));
119       l4re_util_cap_free_um(ev_ds);
120       l4re_util_cap_free_um(ev_irq);
121       return -L4_ENOENT;
122     }
123
124
125   ds_size = l4re_ds_size(ev_ds);
126   if (ds_size < 0)
127     return -L4_ENOMEM;
128
129   if (l4re_event_buffer_attach(&ev_buf, ev_ds, l4re_env()->rm))
130     return -L4_ENOMEM;
131
132   /* create mutex to make dope_cmdf and dope_cmd thread save */
133   dopelib_cmdf_mutex = dopelib_mutex_create(0);
134   dopelib_cmd_mutex  = dopelib_mutex_create(0);
135
136   initialized++;
137   return 0;
138 }
139