1 /* 2 * Semaphore utilities. 3 * 4 * Copyright (C) 2021 Paul Boddie <paul@boddie.org.uk> 5 * 6 * This program is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU General Public License as 8 * published by the Free Software Foundation; either version 2 of 9 * the License, or (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program; if not, write to the Free Software 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, 19 * Boston, MA 02110-1301, USA 20 */ 21 22 #include <l4/sys/err.h> 23 #include <l4/sys/semaphore.h> 24 25 #include "semaphore.h" 26 27 28 29 /* Obtain control of a semaphore. */ 30 31 long ipc_semaphore_down(l4_cap_idx_t semaphore) 32 { 33 long err; 34 35 if (l4_is_valid_cap(semaphore)) 36 { 37 do 38 { 39 /* Since l4_irq_trigger is a "send-only" operation, l4_error is not 40 appropriate. */ 41 42 err = l4_ipc_error(l4_semaphore_down(semaphore, L4_IPC_NEVER), l4_utcb()); 43 44 /* Handle EAGAIN ("Resource temporarily unavailable") by just retrying. */ 45 46 if (err) 47 { 48 if (err == -L4_EAGAIN) 49 continue; 50 else 51 return err; 52 } 53 } 54 while (0); 55 } 56 57 return L4_EOK; 58 } 59 60 /* Relinquish control of a semaphore. */ 61 62 long ipc_semaphore_up(l4_cap_idx_t semaphore) 63 { 64 long err; 65 66 if (l4_is_valid_cap(semaphore)) 67 { 68 do 69 { 70 /* Since l4_irq_trigger is a "send-only" operation, l4_error is not 71 appropriate. */ 72 73 err = l4_ipc_error(l4_semaphore_up(semaphore), l4_utcb()); 74 75 /* Handle EAGAIN ("Resource temporarily unavailable") by just retrying. */ 76 77 if (err) 78 { 79 if (err == -L4_EAGAIN) 80 continue; 81 else 82 return err; 83 } 84 } 85 while (0); 86 } 87 88 return L4_EOK; 89 } 90 91 // vim: tabstop=2 expandtab shiftwidth=2