Sample module to show how to use psrwlock. Signed-off-by: Mathieu Desnoyers CC: Linus Torvalds Cc: "H. Peter Anvin" CC: Jeremy Fitzhardinge CC: Andrew Morton CC: Ingo Molnar CC: "Paul E. McKenney" CC: Peter Zijlstra CC: Joe Perches CC: Wei Weng --- samples/Kconfig | 5 + samples/Makefile | 2 samples/psrwlock/Makefile | 4 samples/psrwlock/psrwlock_example.c | 173 ++++++++++++++++++++++++++++++++++++ 4 files changed, 183 insertions(+), 1 deletion(-) Index: linux-2.6-lttng/samples/Makefile =================================================================== --- linux-2.6-lttng.orig/samples/Makefile 2008-09-06 14:05:34.000000000 -0400 +++ linux-2.6-lttng/samples/Makefile 2008-09-06 14:12:42.000000000 -0400 @@ -1,3 +1,3 @@ # Makefile for Linux samples code -obj-$(CONFIG_SAMPLES) += markers/ kobject/ kprobes/ +obj-$(CONFIG_SAMPLES) += markers/ kobject/ kprobes/ psrwlock/ Index: linux-2.6-lttng/samples/psrwlock/Makefile =================================================================== --- /dev/null 1970-01-01 00:00:00.000000000 +0000 +++ linux-2.6-lttng/samples/psrwlock/Makefile 2008-09-06 14:12:42.000000000 -0400 @@ -0,0 +1,4 @@ +# builds the writer-biased rwlock example kernel modules; +# then to use one (as root): insmod + +obj-$(CONFIG_SAMPLE_PSRWLOCK) += psrwlock_example.o Index: linux-2.6-lttng/samples/psrwlock/psrwlock_example.c =================================================================== --- /dev/null 1970-01-01 00:00:00.000000000 +0000 +++ linux-2.6-lttng/samples/psrwlock/psrwlock_example.c 2008-09-06 14:21:22.000000000 -0400 @@ -0,0 +1,173 @@ +/* + * Priority Sifting Reader-Writer Lock Example + * + * Copyright 2008 Mathieu Desnoyers + */ + +#include +#include + +/* + * Define which execution contexts can access the lock in read or write mode. + * See psrwlock.h and psrwlock-types.h for details. + * + * In this example, the writer is in preemptable context and the readers either + * in IRQ context, softirq context, non-preemptable context or preemptable + * context. + */ +#define SAMPLE_ALL_WCTX PSRW_PRIO_P +#define SAMPLE_ALL_RCTX (PSR_IRQ | PSR_BH | PSR_NPTHREAD | PSR_PTHREAD) + +static DEFINE_PSRWLOCK(sample_rwlock, SAMPLE_ALL_WCTX, SAMPLE_ALL_RCTX); +CHECK_PSRWLOCK_MAP(sample_rwlock, SAMPLE_ALL_WCTX, SAMPLE_ALL_RCTX); + +/* + * Reader in IRQ context. + */ +static void executed_in_irq(void) +{ + psread_lock_irq(&sample_rwlock); + /* read structure */ + psread_unlock(&sample_rwlock); +} + +/* + * Reader in Softirq context. + */ +static void executed_in_bh(void) +{ + psread_lock_bh(&sample_rwlock); + /* read structure */ + psread_unlock(&sample_rwlock); +} + +/* + * Reader in non-preemptable context. + */ +static void executed_inatomic(void) +{ + psread_lock_inatomic(&sample_rwlock); + /* read structure */ + psread_unlock(&sample_rwlock); +} + +/* + * Reader in preemptable context. + */ +static void reader_executed_preemptable(void) +{ + psread_lock(&sample_rwlock); + /* read structure */ + psread_unlock(&sample_rwlock); +} + +/* + * Writer in preemptable context. + */ +static void writer_executed_preemptable(void) +{ + pswrite_lock(&sample_rwlock, SAMPLE_ALL_WCTX, SAMPLE_ALL_RCTX); + /* read structure */ + pswrite_unlock(&sample_rwlock, SAMPLE_ALL_WCTX, SAMPLE_ALL_RCTX); +} + +/* + * Execute readers in all contexts. + */ +static void sample_all_context(void) +{ + local_irq_disable(); + executed_in_irq(); + local_irq_enable(); + + local_bh_disable(); + executed_in_bh(); + local_bh_enable(); + + preempt_disable(); + executed_inatomic(); + preempt_enable(); + + reader_executed_preemptable(); + + writer_executed_preemptable(); +} + + +/* + * In this second example, the writer is in non-preemptable context and the + * readers either in IRQ context or softirq context only. + */ +static DEFINE_PSRWLOCK(sample_wnp_rbh_rirq_rwlock, + PSRW_PRIO_P, PSR_IRQ | PSR_BH); +CHECK_PSRWLOCK_MAP(sample_wnp_rbh_rirq_rwlock, + PSRW_PRIO_P, PSR_IRQ | PSR_BH); + +/* + * Reader in IRQ context. + */ +static void wnp_rbh_rirq_executed_in_irq(void) +{ + psread_lock_irq(&sample_wnp_rbh_rirq_rwlock); + /* read structure */ + psread_unlock(&sample_wnp_rbh_rirq_rwlock); +} + +/* + * Reader in Softirq context. + */ +static void wnp_rbh_rirq_executed_in_bh(void) +{ + psread_lock_bh(&sample_wnp_rbh_rirq_rwlock); + /* read structure */ + psread_unlock(&sample_wnp_rbh_rirq_rwlock); +} + +/* + * Writer in preemptable context. + */ +static void wnp_rbh_rirq_writer_executed_non_preemptable(void) +{ + pswrite_lock(&sample_wnp_rbh_rirq_rwlock, + PSRW_PRIO_P, PSR_IRQ | PSR_BH); + /* read structure */ + pswrite_unlock(&sample_wnp_rbh_rirq_rwlock, + PSRW_PRIO_P, PSR_IRQ | PSR_BH); +} + +/* + * Execute readers in all contexts. + */ +static void sample_wnp_rbh_rirq_context(void) +{ + local_irq_disable(); + wnp_rbh_rirq_executed_in_irq(); + local_irq_enable(); + + local_bh_disable(); + wnp_rbh_rirq_executed_in_bh(); + local_bh_enable(); + + preempt_disable(); + wnp_rbh_rirq_writer_executed_non_preemptable(); + preempt_enable(); +} + +static int __init init_example(void) +{ + sample_all_context(); + sample_wnp_rbh_rirq_context(); + + return 0; +} + +static void __exit exit_example(void) +{ +} + +module_init(init_example) +module_exit(exit_example) + +MODULE_LICENSE("GPL"); +MODULE_AUTHOR("Mathieu Desnoyers"); +MODULE_DESCRIPTION("psrwlock example"); Index: linux-2.6-lttng/samples/Kconfig =================================================================== --- linux-2.6-lttng.orig/samples/Kconfig 2008-09-06 14:05:34.000000000 -0400 +++ linux-2.6-lttng/samples/Kconfig 2008-09-06 14:12:42.000000000 -0400 @@ -33,5 +33,10 @@ config SAMPLE_KRETPROBES default m depends on SAMPLE_KPROBES && KRETPROBES +config SAMPLE_PSRWLOCK + tristate "Build psrwlock example -- loadable modules only" + default m + depends on m + endif # SAMPLES -- Mathieu Desnoyers Computer Engineering Ph.D. Student, Ecole Polytechnique de Montreal OpenPGP key fingerprint: 8CD5 52C3 8E3C 4140 715F BA06 3F25 A8FE 3BAE 9A68 -- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/