source: CIVL/examples/omp/dataracebench-1.3.2/micro-benchmarks/DRB119-nestlock-orig-yes.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.3 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/*
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 48 and line 53. So, it needs a nest_lock enclosing line 32
14Missing nest_lock will lead to race condition at line:32.
15Data Race Pairs, p->b@32:2 and p->b@32:2.
16*/
17
18#include <omp.h>
19#include <stdio.h>
20#include <stdlib.h>
21
22typedef struct {
23 int a, b;
24 omp_nest_lock_t lck;
25} pair;
26
27void incr_a(pair *p){
28 p->a += 1;
29}
30
31void incr_b(pair *p){
32 p->b += 1;
33}
34
35
36int main(int argc, char* argv[])
37{
38 pair p[1];
39 p->a = 0;
40 p->b = 0;
41 omp_init_nest_lock(&p->lck);
42
43 #pragma omp parallel sections
44 {
45 #pragma omp section
46 {
47 omp_set_nest_lock(&p->lck);
48 incr_b(p);
49 incr_a(p);
50 omp_unset_nest_lock(&p->lck);
51 }
52 #pragma omp section
53 incr_b(p);
54 }
55
56 omp_destroy_nest_lock(&p->lck);
57
58 printf("%d\n",p->b);
59 return 0;
60}
Note: See TracBrowser for help on using the repository browser.