Home | History | Annotate | Download | only in threading
      1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "ppapi/utility/threading/lock.h"
      6 
      7 namespace pp {
      8 
      9 #ifdef WIN32  // Windows implementation for native plugins.
     10 
     11 Lock::Lock() {
     12   // The second parameter is the spin count; for short-held locks it avoids the
     13   // contending thread from going to sleep which helps performance greatly.
     14   ::InitializeCriticalSectionAndSpinCount(&os_lock_, 2000);
     15 }
     16 
     17 Lock::~Lock() {
     18   ::DeleteCriticalSection(&os_lock_);
     19 }
     20 
     21 void Lock::Acquire() {
     22   ::EnterCriticalSection(&os_lock_);
     23 }
     24 
     25 void Lock::Release() {
     26   ::LeaveCriticalSection(&os_lock_);
     27 }
     28 
     29 #else  // Posix implementation.
     30 
     31 Lock::Lock() {
     32   pthread_mutex_init(&os_lock_, NULL);
     33 }
     34 
     35 Lock::~Lock() {
     36   pthread_mutex_destroy(&os_lock_);
     37 }
     38 
     39 void Lock::Acquire() {
     40   pthread_mutex_lock(&os_lock_);
     41 }
     42 
     43 void Lock::Release() {
     44   pthread_mutex_unlock(&os_lock_);
     45 }
     46 
     47 #endif
     48 
     49 }  // namespace pp
     50