bart | 78bfc71 | 2011-12-08 16:14:59 +0000 | [diff] [blame] | 1 | |
| 2 | /*--------------------------------------------------------------------*/ |
| 3 | /*--- Generic scheduler lock implementation sched-lock-generic.c ---*/ |
| 4 | /*--- ---*/ |
| 5 | /*--- This implementation does not guarantee fair scheduling on ---*/ |
| 6 | /*--- multicore systems but is sufficient to make the Valgrind ---*/ |
| 7 | /*--- scheduler work reasonably. ---*/ |
| 8 | /*--------------------------------------------------------------------*/ |
| 9 | |
| 10 | /* |
| 11 | This file is part of Valgrind, a dynamic binary instrumentation |
| 12 | framework. |
| 13 | |
sewardj | 0f157dd | 2013-10-18 14:27:36 +0000 | [diff] [blame] | 14 | Copyright (C) 2011-2013 Bart Van Assche <bvanassche@acm.org>. |
bart | 78bfc71 | 2011-12-08 16:14:59 +0000 | [diff] [blame] | 15 | |
| 16 | This program is free software; you can redistribute it and/or |
| 17 | modify it under the terms of the GNU General Public License as |
| 18 | published by the Free Software Foundation; either version 2 of the |
| 19 | License, or (at your option) any later version. |
| 20 | |
| 21 | This program is distributed in the hope that it will be useful, but |
| 22 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 23 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 24 | General Public License for more details. |
| 25 | |
| 26 | You should have received a copy of the GNU General Public License |
| 27 | along with this program; if not, write to the Free Software |
| 28 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 29 | 02111-1307, USA. |
| 30 | |
| 31 | The GNU General Public License is contained in the file COPYING. |
| 32 | */ |
| 33 | |
| 34 | #include "pub_core_basics.h" |
florian | c91f584 | 2013-09-15 10:42:26 +0000 | [diff] [blame] | 35 | #include "pub_core_mallocfree.h" |
bart | 78bfc71 | 2011-12-08 16:14:59 +0000 | [diff] [blame] | 36 | #include "priv_sema.h" |
| 37 | #include "priv_sched-lock.h" |
| 38 | #include "priv_sched-lock-impl.h" |
| 39 | |
| 40 | struct sched_lock { |
| 41 | vg_sema_t sema; |
| 42 | }; |
| 43 | |
florian | cd19e99 | 2012-11-03 19:32:28 +0000 | [diff] [blame] | 44 | static const HChar *get_sched_lock_name(void) |
bart | 78bfc71 | 2011-12-08 16:14:59 +0000 | [diff] [blame] | 45 | { |
| 46 | return "generic"; |
| 47 | } |
| 48 | |
| 49 | static struct sched_lock *create_sched_lock(void) |
| 50 | { |
| 51 | struct sched_lock *p; |
| 52 | |
| 53 | p = VG_(malloc)("sched_lock", sizeof(*p)); |
| 54 | if (p) |
| 55 | ML_(sema_init)(&p->sema); |
| 56 | return p; |
| 57 | } |
| 58 | |
| 59 | static void destroy_sched_lock(struct sched_lock *p) |
| 60 | { |
| 61 | ML_(sema_deinit)(&p->sema); |
| 62 | VG_(free)(p); |
| 63 | } |
| 64 | |
| 65 | static int get_sched_lock_owner(struct sched_lock *p) |
| 66 | { |
| 67 | return p->sema.owner_lwpid; |
| 68 | } |
| 69 | |
| 70 | static void acquire_sched_lock(struct sched_lock *p) |
| 71 | { |
| 72 | ML_(sema_down)(&p->sema, False); |
| 73 | } |
| 74 | |
| 75 | static void release_sched_lock(struct sched_lock *p) |
| 76 | { |
| 77 | ML_(sema_up)(&p->sema, False); |
| 78 | } |
| 79 | |
| 80 | const struct sched_lock_ops ML_(generic_sched_lock_ops) = { |
| 81 | .get_sched_lock_name = get_sched_lock_name, |
| 82 | .create_sched_lock = create_sched_lock, |
| 83 | .destroy_sched_lock = destroy_sched_lock, |
| 84 | .get_sched_lock_owner = get_sched_lock_owner, |
| 85 | .acquire_sched_lock = acquire_sched_lock, |
| 86 | .release_sched_lock = release_sched_lock, |
| 87 | }; |