github.com/golang/gofrontend@v0.0.0-20240429183944-60f985a78526/libgo/runtime/yield.c (about)

     1  // Copyright 2011 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  #include "config.h"
     6  
     7  #include <stddef.h>
     8  #include <sys/types.h>
     9  #include <sys/time.h>
    10  #include <sched.h>
    11  #include <unistd.h>
    12  
    13  #ifdef HAVE_SYS_SELECT_H
    14  #include <sys/select.h>
    15  #endif
    16  
    17  #if defined (__i386__) || defined (__x86_64__)
    18  #include <xmmintrin.h>
    19  #endif
    20  
    21  #include "runtime.h"
    22  
    23  /* Spin wait.  */
    24  
    25  void
    26  runtime_procyield (uint32 cnt)
    27  {
    28    volatile uint32 i;
    29  
    30    for (i = 0; i < cnt; ++i)
    31      {
    32  #if defined (__i386__) || defined (__x86_64__)
    33        _mm_pause ();
    34  #endif
    35      }
    36  }
    37  
    38  /* Ask the OS to reschedule this thread.  */
    39  
    40  void runtime_osyield(void)
    41    __attribute__ ((no_split_stack));
    42  
    43  void
    44  runtime_osyield (void)
    45  {
    46    sched_yield ();
    47  }
    48  
    49  /* Sleep for some number of microseconds.  */
    50  
    51  void
    52  runtime_usleep (uint32 us)
    53  {
    54    struct timeval tv;
    55  
    56    tv.tv_sec = us / 1000000;
    57    tv.tv_usec = us % 1000000;
    58    select (0, NULL, NULL, NULL, &tv);
    59  }