source: CIVL/examples/omp/dataracebench-1.3.2/micro-benchmarks/original/DRB118-nestlock-orig-no.c

main
Last change on this file was ea777aa, checked in by Alex Wilton <awilton@…>, 3 years ago

Moved examples, include, build_default.properties, common.xml, and README out from dev.civl.com into the root of the repo.

git-svn-id: svn://vsl.cis.udel.edu/civl/trunk@5704 fb995dde-84ed-4084-dfe6-e5aef3e2452c

  • Property mode set to 100755
File size: 1.4 KB
Line 
1/*
2!!!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~!!!
3!!! Copyright (c) 2017-20, Lawrence Livermore National Security, LLC
4!!! and DataRaceBench project contributors. See the DataRaceBench/COPYRIGHT file for details.
5!!!
6!!! SPDX-License-Identifier: (BSD-3-Clause)
7!!!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~!!!
8*/
9
10/* This example is modified version of nestable_lock.1.c example, OpenMP 5.0 Application Programming Examples.
11A nested lock can be locked several times. It doesn't unlock until you have unset
12it as many times as the number of calls to omp_set_nest_lock.
13incr_b is called at line 47 and line 52. So, it needs a nest_lock for p->b@30:4.
14*/
15
16#include <omp.h>
17#include <stdio.h>
18#include <stdlib.h>
19
20typedef struct {
21 int a, b;
22 omp_nest_lock_t lck;
23} pair;
24
25void incr_a(pair *p){
26 p->a += 1;
27}
28void incr_b(pair *p){
29 omp_set_nest_lock(&p->lck);
30 p->b += 1;
31 omp_unset_nest_lock(&p->lck);
32}
33
34
35int main(int argc, char* argv[])
36{
37 pair p[1];
38 p->a = 0;
39 p->b = 0;
40 omp_init_nest_lock(&p->lck);
41
42 #pragma omp parallel sections
43 {
44 #pragma omp section
45 {
46 omp_set_nest_lock(&p->lck);
47 incr_b(p);
48 incr_a(p);
49 omp_unset_nest_lock(&p->lck);
50 }
51 #pragma omp section
52 incr_b(p);
53 }
54
55 omp_destroy_nest_lock(&p->lck);
56
57 printf("%d\n",p->b);
58 return 0;
59}
Note: See TracBrowser for help on using the repository browser.