source: LMDZ6/branches/LMDZ-ECRAD/libf/phylmd/ecrad/cas.h @ 3880

Last change on this file since 3880 was 3880, checked in by idelkadi, 3 years ago

Online implementation of the radiative transfer code ECRAD in LMDZ.

  • Inclusion of the ecrad directory containing the sources of the ECRAD code
  • Adaptation of compilation scripts (CPP_ECRAD keys)
  • Call of ecrad in radlwsw_m.F90 under the logical key iflag_rrtm = 2
File size: 1.3 KB
Line 
1/**
2 * (C) Copyright 2014- ECMWF.
3 *
4 * This software is licensed under the terms of the Apache Licence Version 2.0
5 * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
6 *
7 * In applying this licence, ECMWF does not waive the privileges and immunities
8 * granted to it by virtue of its status as an intergovernmental organisation
9 * nor does it submit to any jurisdiction.
10 */
11
12// cas.h
13//
14// compare_and_swap -based locks
15//
16// Thanks to https://github.com/majek/dump/blob/master/msqueue/queue_lock_myspinlock1.c
17//
18
19#include <signal.h>
20#include <sched.h>
21
22#ifndef INLINE
23#define INLINE __inline__
24#endif
25
26#if defined(__GNUC__) && !defined(__NEC__)
27
28#define CAS(lock,oldval,newval) __sync_bool_compare_and_swap(lock,oldval,newval)
29
30#else
31
32#warning *** CAS-locks self-implemented ***
33
34static INLINE int CAS(volatile sig_atomic_t *lock, int oldval, int newval)
35{
36  int tmp = *lock;
37  if (tmp == oldval) *lock = newval;
38  return tmp;
39}
40
41#endif
42
43static INLINE void cas_init(volatile sig_atomic_t *lock)
44{
45  if (lock) *lock = 0;
46}
47
48static INLINE void cas_lock(volatile sig_atomic_t *lock) 
49{
50  while (1) {
51    int i;
52    for (i=0; i < 10000; ++i) {
53      if (CAS(lock, 0, 1)) return;
54    }
55    sched_yield();
56  }
57}
58
59static INLINE void cas_unlock(volatile sig_atomic_t *lock) 
60{
61  CAS(lock, 1, 0);
62}
Note: See TracBrowser for help on using the repository browser.