1
2/*--------------------------------------------------------------------*/
3/*--- Scheduler lock support functions                sched-lock.c ---*/
4/*--------------------------------------------------------------------*/
5
6/*
7   This file is part of Valgrind, a dynamic binary instrumentation
8   framework.
9
10   Copyright (C) 2011-2013 Bart Van Assche <bvanassche@acm.org>.
11
12   This program is free software; you can redistribute it and/or
13   modify it under the terms of the GNU General Public License as
14   published by the Free Software Foundation; either version 2 of the
15   License, or (at your option) any later version.
16
17   This program is distributed in the hope that it will be useful, but
18   WITHOUT ANY WARRANTY; without even the implied warranty of
19   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20   General Public License for more details.
21
22   You should have received a copy of the GNU General Public License
23   along with this program; if not, write to the Free Software
24   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
25   02111-1307, USA.
26
27   The GNU General Public License is contained in the file COPYING.
28*/
29
30#include "config.h"
31#include "pub_core_basics.h"
32#include "pub_core_libcbase.h"
33#include "pub_core_mallocfree.h"
34#include "priv_sema.h"
35#include "priv_sched-lock.h"
36#include "priv_sched-lock-impl.h"
37
38static struct sched_lock_ops const *sched_lock_ops =
39   &ML_(generic_sched_lock_ops);
40
41static struct sched_lock_ops const *const sched_lock_impl[] = {
42   [sched_lock_generic] = &ML_(generic_sched_lock_ops),
43#ifdef ENABLE_LINUX_TICKET_LOCK
44   [sched_lock_ticket]  = &ML_(linux_ticket_lock_ops),
45#endif
46};
47
48/**
49 * Define which scheduler lock implementation to use.
50 *
51 * @param[in] t Scheduler lock type.
52 *
53 * @return True if and only if this function succeeded.
54 *
55 * @note Must be called before any other sched_lock*() function is invoked.
56 */
57Bool ML_(set_sched_lock_impl)(const enum SchedLockType t)
58{
59   struct sched_lock_ops const *p = NULL;
60
61   if ((unsigned)t < sizeof(sched_lock_impl)/sizeof(sched_lock_impl[0]))
62      p = sched_lock_impl[t];
63   if (p)
64      sched_lock_ops = p;
65   return !!p;
66}
67
68const HChar *ML_(get_sched_lock_name)(void)
69{
70   return (sched_lock_ops->get_sched_lock_name)();
71}
72
73struct sched_lock *ML_(create_sched_lock)(void)
74{
75   return (sched_lock_ops->create_sched_lock)();
76}
77
78void ML_(destroy_sched_lock)(struct sched_lock *p)
79{
80   return (sched_lock_ops->destroy_sched_lock)(p);
81}
82
83int ML_(get_sched_lock_owner)(struct sched_lock *p)
84{
85   return (sched_lock_ops->get_sched_lock_owner)(p);
86}
87
88void ML_(acquire_sched_lock)(struct sched_lock *p)
89{
90   return (sched_lock_ops->acquire_sched_lock)(p);
91}
92
93void ML_(release_sched_lock)(struct sched_lock *p)
94{
95   return (sched_lock_ops->release_sched_lock)(p);
96}
97