1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
|
/*
* os_handler.c
*
* MontaVista IPMI os handler tools.
*
* Author: MontaVista Software, Inc.
* Corey Minyard <minyard@mvista.com>
* source@mvista.com
*
* Copyright 2006 MontaVista Software Inc.
*
* This software is available to you under a choice of one of two
* licenses. You may choose to be licensed under the terms of the GNU
* Lesser General Public License (GPL) Version 2 or the modified BSD
* license below. The following disclamer applies to both licenses:
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
* USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* GNU Lesser General Public Licence
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program; if not, write to the Free
* Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* Modified BSD Licence
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* 3. The name of the author may not be used to endorse or promote
* products derived from this software without specific prior
* written permission.
*/
#include <stdlib.h>
#include <string.h>
#include <OpenIPMI/os_handler.h>
#include <OpenIPMI/internal/ipmi_malloc.h>
#include <errno.h>
/*
* This code is subtle, be careful.
*
* We handle three basic modes here:
*
* * single-threaded OS handler
* * multi-threaded OS handler, multi-threaded (num_threads > 0)
* * multi-threaded OS handler, single threaded support (num_threads == 0)
* (multi-single mode)
*
* Single threaded OS handler is simple. We just run the event loop
* until the wait condition is done.
*
* Full multi-threaded mode is simple, too. We allocate threads to
* run the event loop, then we use condition variables to operate.
*
* Multi-single mode is not simple. You can't just run the event
* loop. Another thread may be running the event loop, too, and thus
* handle the particular operation that frees up our event loop (thus
* we will still be stuck waiting in our event loop). We can't have
* another thread arbitrarily run the event loop, as we need to
* support single-threaded system.
*
* So what we do is allocate a single thread. It runs the event loop
* *only when something is waiting*. Thus we are single-threaded if
* the app is single threaded. But since condition variables are used
* for wakeups, we can support multi-threaded applications properly.
*/
struct os_handler_waiter_factory_s
{
os_handler_t *os_hnd;
unsigned int num_threads;
int thread_priority;
int has_threads;
os_hnd_lock_t *lock;
os_hnd_cond_t *cond;
/* Number of currently running threads. */
unsigned int thread_count;
/* Number of wait structures we have out for this factory. */
unsigned int num_waiters;
/* Tells the main thread to stop. */
volatile int stop_threads;
/* Number of single-thread users. */
unsigned int single_thread_use_count;
os_hnd_cond_t *single_thread_cond;
};
struct os_handler_waiter_s
{
os_handler_waiter_factory_t *factory;
os_hnd_lock_t *lock;
os_hnd_cond_t *cond;
/* Am I using multi-single mode and waiting? */
int is_single;
unsigned int count;
};
/* This is a normal event loop thread for full multi-threaded mode. */
static void
waiter_thread(void *data)
{
os_handler_waiter_factory_t *factory = data;
os_handler_t *os_hnd = factory->os_hnd;
while (!factory->stop_threads) {
struct timeval tv = { 1, 0 };
os_hnd->perform_one_op(os_hnd, &tv);
}
os_hnd->lock(os_hnd, factory->lock);
factory->thread_count--;
if (factory->thread_count == 0)
os_hnd->cond_wake(os_hnd, factory->cond);
os_hnd->unlock(os_hnd, factory->lock);
}
/* Event loop thread for multi-single mode. Subtle, be careful and
read the comments. */
static void
single_waiter_thread(void *data)
{
os_handler_waiter_factory_t *factory = data;
os_handler_t *os_hnd = factory->os_hnd;
os_hnd->lock(os_hnd, factory->lock);
while (!factory->stop_threads) {
/* While things are waiting, run out thread. This is subtle
here. If we are truely single-threaded, the thing that
releases the wait is going to be running from this thread.
This single_thread_use_count will be set to zero before
returning from perform_one_op, and we just quit and wait to
be woken again and won't call more event loop operations.
If we are not single-threaded (the app is multi-threaded)
we still run from here, but the app better be ready for
multi-threaded operation. */
while (factory->single_thread_use_count) {
struct timeval tv = { 1, 0 };
os_hnd->unlock(os_hnd, factory->lock);
os_hnd->perform_one_op(os_hnd, &tv);
os_hnd->lock(os_hnd, factory->lock);
}
/* Wait for someone to tell us there are more event to run */
os_hnd->cond_wait(os_hnd, factory->single_thread_cond, factory->lock);
}
factory->thread_count--;
if (factory->thread_count == 0)
os_hnd->cond_wake(os_hnd, factory->cond);
os_hnd->unlock(os_hnd, factory->lock);
}
int
os_handler_alloc_waiter_factory(os_handler_t *os_hnd,
unsigned int num_threads,
int thread_priority,
os_handler_waiter_factory_t **factory)
{
os_handler_waiter_factory_t *nf;
int rv;
unsigned int i;
int has_threads = 0;
ipmi_malloc_init(os_hnd);
if (os_hnd->create_lock && os_hnd->create_cond
&& os_hnd->create_thread)
has_threads = 1;
if ((num_threads > 0) && !has_threads) {
/* Asked for threads, but handler doesn't support them. */
return ENOSYS;
}
nf = ipmi_mem_alloc(sizeof(*nf));
if (!nf)
return ENOMEM;
memset(nf, 0, sizeof(*nf));
nf->has_threads = has_threads;
nf->os_hnd = os_hnd;
nf->thread_priority = thread_priority;
nf->num_threads = num_threads;
if (has_threads) {
rv = os_hnd->create_lock(os_hnd, &nf->lock);
if (rv) {
ipmi_mem_free(nf);
return rv;
}
rv = os_hnd->create_cond(os_hnd, &nf->cond);
if (rv) {
os_hnd->destroy_lock(os_hnd, nf->lock);
ipmi_mem_free(nf);
return rv;
}
}
if (num_threads > 0) {
for (i=0; i<num_threads; i++) {
nf->thread_count++;
rv = os_hnd->create_thread(os_hnd, thread_priority,
waiter_thread, nf);
if (rv) {
nf->thread_count--;
os_handler_free_waiter_factory(nf);
return rv;
}
}
} else if (has_threads) {
rv = os_hnd->create_cond(os_hnd, &nf->single_thread_cond);
if (rv) {
os_handler_free_waiter_factory(nf);
return rv;
}
nf->thread_count++;
rv = os_hnd->create_thread(os_hnd, thread_priority,
single_waiter_thread, nf);
if (rv) {
nf->thread_count--;
os_handler_free_waiter_factory(nf);
return rv;
}
}
*factory = nf;
return 0;
}
void
os_handler_global_shutdown(void)
{
ipmi_malloc_shutdown();
}
int
os_handler_free_waiter_factory(os_handler_waiter_factory_t *factory)
{
os_handler_t *os_hnd = factory->os_hnd;
if (factory->lock)
os_hnd->lock(os_hnd, factory->lock);
if (factory->stop_threads)
return EINVAL;
if (factory->num_waiters > 0)
return EAGAIN;
if (factory->thread_count > 0) {
factory->stop_threads = 1;
if (factory->single_thread_cond)
os_hnd->cond_wake(os_hnd, factory->single_thread_cond);
os_hnd->cond_wait(os_hnd, factory->cond, factory->lock);
}
if (factory->has_threads) {
os_hnd->unlock(os_hnd, factory->lock);
os_hnd->destroy_lock(os_hnd, factory->lock);
os_hnd->destroy_cond(os_hnd, factory->cond);
}
if (factory->single_thread_cond)
os_hnd->destroy_cond(os_hnd, factory->single_thread_cond);
ipmi_mem_free(factory);
return 0;
}
os_handler_waiter_t *
os_handler_alloc_waiter(os_handler_waiter_factory_t *factory)
{
os_handler_waiter_t *nw;
os_handler_t *os_hnd = factory->os_hnd;
int rv;
nw = ipmi_mem_alloc(sizeof(*nw));
if (!nw)
return NULL;
memset(nw, 0, sizeof(*nw));
nw->factory = factory;
if (factory->has_threads) {
rv = os_hnd->create_lock(os_hnd, &nw->lock);
if (rv) {
ipmi_mem_free(nw);
return NULL;
}
rv = os_hnd->create_cond(os_hnd, &nw->cond);
if (rv) {
os_hnd->destroy_lock(os_hnd, nw->lock);
ipmi_mem_free(nw);
return NULL;
}
}
if (factory->lock)
os_hnd->lock(os_hnd, factory->lock);
factory->num_waiters++;
if (factory->lock)
os_hnd->unlock(os_hnd, factory->lock);
nw->count = 1;
return nw;
}
int
os_handler_free_waiter(os_handler_waiter_t *waiter)
{
os_handler_t *os_hnd = waiter->factory->os_hnd;
if (waiter->count > 0)
return EAGAIN;
if (waiter->factory->lock)
os_hnd->lock(os_hnd, waiter->factory->lock);
waiter->factory->num_waiters--;
if (waiter->factory->lock)
os_hnd->unlock(os_hnd, waiter->factory->lock);
if (waiter->lock)
os_hnd->destroy_lock(os_hnd, waiter->lock);
if (waiter->cond)
os_hnd->destroy_cond(os_hnd, waiter->cond);
ipmi_mem_free(waiter);
return 0;
}
void
os_handler_waiter_use(os_handler_waiter_t *waiter)
{
os_handler_t *os_hnd = waiter->factory->os_hnd;
if (waiter->lock)
os_hnd->lock(os_hnd, waiter->lock);
waiter->count++;
if (waiter->lock)
os_hnd->unlock(os_hnd, waiter->lock);
}
void
os_handler_waiter_release(os_handler_waiter_t *waiter)
{
os_handler_t *os_hnd = waiter->factory->os_hnd;
if (waiter->lock)
os_hnd->lock(os_hnd, waiter->lock);
if (waiter->count == 0) {
os_hnd->log(os_hnd, IPMI_LOG_SEVERE,
"os_handler_waiter_release: Got a release when the"
" wait count was already zero");
} else {
waiter->count--;
if (waiter->lock && (waiter->count == 0)) {
if (waiter->is_single) {
/* We handle the single thread count here and not in
the waiter to avoid a race condition. See comments
at the beginning of this file. */
os_hnd->lock(os_hnd, waiter->factory->lock);
waiter->factory->single_thread_use_count--;
os_hnd->unlock(os_hnd, waiter->factory->lock);
waiter->is_single = 0;
}
os_hnd->cond_wake(os_hnd, waiter->cond);
}
}
if (waiter->lock)
os_hnd->unlock(os_hnd, waiter->lock);
}
int
os_handler_waiter_wait(os_handler_waiter_t *waiter, struct timeval *timeout)
{
os_handler_waiter_factory_t *factory = waiter->factory;
os_handler_t *os_hnd = waiter->factory->os_hnd;
int rv = 0;
if (waiter->lock) {
os_hnd->lock(os_hnd, waiter->lock);
if (waiter->count > 0) {
if (factory->num_threads == 0) {
/* Threaded, but we don't have a simultaneous running
event loop. */
os_hnd->lock(os_hnd, factory->lock);
if (factory->single_thread_use_count == 0) {
/* Wake the event loop thread. */
os_hnd->cond_wake(os_hnd, factory->single_thread_cond);
}
factory->single_thread_use_count++;
os_hnd->unlock(os_hnd, factory->lock);
waiter->is_single = 1;
}
rv = os_hnd->cond_timedwait(os_hnd, waiter->cond,
waiter->lock, timeout);
/* single_thread_use_count is decremented by the waker
unless it failes to receive the wakeup. */
if (rv)
factory->single_thread_use_count--;
}
os_hnd->unlock(os_hnd, waiter->lock);
} else {
while (waiter->count > 0)
os_hnd->perform_one_op(os_hnd, timeout);
}
return rv;
}
|