title: pthread_mutex_timedlock use from Linux assembly language
body: <h2>The setting:</h2>
  
  <p>I am trying to use <code>pthread_mutex_timedlock</code> function from <code>pthreads</code> in my 32 bit assembly language program. The code looks following way:</p>
  
  <pre><code>struct timespec
    .tv_sec  dd ?   ; time in seconds
    .tv_nsec dd ?   ; time is nano seconds
  ends
  
  ;....
  .time timespec  ; the timespec structure
  ;....
  ; the code where pthread_mutex_timedlock is used
  
      mov     eax, [.timeout] ; the timeout in [ms]
      mov     ecx, 1000
      cdq
      div     ecx           ; the timeout in eax [s]
      imul    edx, 1000000  ; the remainder in edx [ns]
  
      mov     [.time.tv_sec], eax
      mov     [.time.tv_nsec], edx
  
      lea     eax, [.time]
      cinvoke pthread_mutex_timedlock, [.ptrMutex], eax
      test    eax, eax
      jnz     .error
  </code></pre>
  
  <p>The problem is that the <code>pthread_mutex_timedlock</code> function locks the mutex only if it is immediately unlocked. </p>
  
  <p>If the mutex is locked in this moment, the function <code>pthread_mutex_timedlock</code> returns immediately with <code>ETIMEDOUT</code> error, without waiting for timeout, ignoring the values set in the <code>timespec</code> structure.</p>
  
  <h2>The question:</h2>
  
  <p>What I am doing wrong?</p>