UNIXworkcode

1 /* 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 3 * 4 * Copyright 2013 Olaf Wintermann. All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions are met: 8 * 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #include "../../ucx/map.h" 30 #include "../util/atomic.h" 31 32 #include "event.h" 33 34 UcxMap *event_handler_map = NULL; 35 int numevhandlers = 0; 36 37 EVHandler *default_event_handler = NULL; 38 39 EVHandler *last_handler_c = NULL; 40 41 int create_event_handler(EventHandlerConfig *cfg) { 42 if(event_handler_map == NULL) { 43 event_handler_map = ucx_map_new(16); 44 } 45 46 /* if the event handler already exists, we don't modify it */ 47 if(ucx_map_sstr_get(event_handler_map, cfg->name)) { 48 /* TODO: log message */ 49 /* TODO: set reload status */ 50 return 1; 51 } 52 53 /* create new handler */ 54 EVHandler *e = evhandler_create(cfg); 55 if(e == NULL) { 56 return 1; 57 } 58 59 if(cfg->isdefault) { 60 if(default_event_handler) { 61 /* there can be only one default event handler */ 62 /* TODO: log warning */ 63 } 64 default_event_handler = e; 65 } 66 67 int ret = ucx_map_sstr_put(event_handler_map, cfg->name, e); 68 if(ret == 0) { 69 last_handler_c = e; 70 numevhandlers++; 71 } 72 73 return ret; 74 } 75 76 /* 77 * checks if there is at least one event handler and a default handler 78 * if necessary, check_event_handler_cfg() creates a default event handler 79 */ 80 int check_event_handler_cfg() { 81 if(numevhandlers > 0 ) { 82 if(default_event_handler) { 83 return 0; 84 } else { 85 default_event_handler = last_handler_c; 86 return 0; 87 } 88 } 89 90 EventHandlerConfig cfg; 91 cfg.name = sstr("default"); 92 cfg.nthreads = 1; 93 cfg.isdefault = 1; 94 95 return create_event_handler(&cfg); 96 } 97 98 99 EVHandler* get_default_event_handler() { 100 return default_event_handler; 101 } 102 103 EVHandler* get_event_handler(char *name) { 104 return ucx_map_cstr_get(event_handler_map, name); 105 } 106 107 EventHandler* ev_instance(EVHandler *ev) { 108 int nev = ev->numins; 109 if(nev == 1) { 110 return ev->instances[0]; 111 } 112 113 int ins = ev->current & nev; 114 ws_atomic_inc32(&ev->current); 115 return ev->instances[ins]; 116 } 117