From patchwork Thu Jun 20 11:24:02 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?q?Michal_Jire=C5=A1?= X-Patchwork-Id: 1950082 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=suse.cz header.i=@suse.cz header.a=rsa-sha256 header.s=susede2_rsa header.b=c4cdcv4b; dkim=pass header.d=suse.cz header.i=@suse.cz header.a=ed25519-sha256 header.s=susede2_ed25519 header.b=3ZRDG3GG; dkim=pass (1024-bit key) header.d=suse.cz header.i=@suse.cz header.a=rsa-sha256 header.s=susede2_rsa header.b=c4cdcv4b; dkim=neutral header.d=suse.cz header.i=@suse.cz header.a=ed25519-sha256 header.s=susede2_ed25519 header.b=3ZRDG3GG; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4W4dS106dmz20X6 for ; Thu, 20 Jun 2024 21:24:33 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 3967D389043D for ; Thu, 20 Jun 2024 11:24:31 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from smtp-out2.suse.de (smtp-out2.suse.de [IPv6:2a07:de40:b251:101:10:150:64:2]) by sourceware.org (Postfix) with ESMTPS id E3F963891C3E for ; Thu, 20 Jun 2024 11:24:05 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E3F963891C3E Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=suse.cz Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=suse.cz ARC-Filter: OpenARC Filter v1.0.0 sourceware.org E3F963891C3E Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a07:de40:b251:101:10:150:64:2 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1718882648; cv=none; b=gljloBxmFuSVgqcrA+daNJpfr5RQZ5kaitWuMAmY+v3AtNoSC5ijGYgJarNWyv1Ofw3/bOo6cOB2PHkTv0q/GtyI4AIfE5DMGIG5/tEv9G7zIty5obGQuLtVAJzKUhhPbNhdNfmh8rIuYzPHrOJSdv82jvJ5cKymUwDBRyhQfww= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1718882648; c=relaxed/simple; bh=GaZ3ghxzF/ie1zPeORyU2/KaP5k+yPK2yIsClcqPcQk=; h=DKIM-Signature:DKIM-Signature:DKIM-Signature:DKIM-Signature:Date: From:To:Subject:Message-ID:MIME-Version; b=jnRUincJmkh0wI3yTFfBVGXdchRqLDQJw31tMGXH+JTORdzL9OI+W4bykwQ4/rrKm8jvdpehF/XNbM8rcDdkLIbyTx14av2Sad8qtblRT8TLYgjsfxWDJrF7T4IRaODesrPQvZK42L6o8tz/4VdCNARnjSXcpNai6D33hBcL9kM= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from imap1.dmz-prg2.suse.org (imap1.dmz-prg2.suse.org [IPv6:2a07:de40:b281:104:10:150:64:97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id 8435D1F455; Thu, 20 Jun 2024 11:24:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1718882643; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=9mpN/6duWHv5dnLjRM1QYD+jF2t7ga9IXzApK+6379A=; b=c4cdcv4b3PyNUOD8zHbiD28LSltPYvSIpz8QZkncvQqecHqjqGqFuGks28DxYGv6GCMYgL RvJ3ezETbDsL+bbSt+qLouaJ5OIC3TWMyuz4gnoTxCbNWNGHCc4NmQkLkDHKs1n2Izrqsq XvxjLjKBfIH4gNAxrRME7jjcCp2WbbU= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1718882643; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=9mpN/6duWHv5dnLjRM1QYD+jF2t7ga9IXzApK+6379A=; b=3ZRDG3GGbrwTKugGyQulPUB1k7+zllOXzafd1S9xrZwNj9sTgz3XuV8lcESWgaFs/2ov5l kiKUMyoZBOs8BfAg== Authentication-Results: smtp-out2.suse.de; dkim=pass header.d=suse.cz header.s=susede2_rsa header.b=c4cdcv4b; dkim=pass header.d=suse.cz header.s=susede2_ed25519 header.b=3ZRDG3GG DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1718882643; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=9mpN/6duWHv5dnLjRM1QYD+jF2t7ga9IXzApK+6379A=; b=c4cdcv4b3PyNUOD8zHbiD28LSltPYvSIpz8QZkncvQqecHqjqGqFuGks28DxYGv6GCMYgL RvJ3ezETbDsL+bbSt+qLouaJ5OIC3TWMyuz4gnoTxCbNWNGHCc4NmQkLkDHKs1n2Izrqsq XvxjLjKBfIH4gNAxrRME7jjcCp2WbbU= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1718882643; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=9mpN/6duWHv5dnLjRM1QYD+jF2t7ga9IXzApK+6379A=; b=3ZRDG3GGbrwTKugGyQulPUB1k7+zllOXzafd1S9xrZwNj9sTgz3XuV8lcESWgaFs/2ov5l kiKUMyoZBOs8BfAg== Received: from imap1.dmz-prg2.suse.org (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id 71D1613AC1; Thu, 20 Jun 2024 11:24:03 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap1.dmz-prg2.suse.org with ESMTPSA id 1x93G1MRdGa+HwAAD6G6ig (envelope-from ); Thu, 20 Jun 2024 11:24:03 +0000 Date: Thu, 20 Jun 2024 13:24:02 +0200 From: Michal Jires To: gcc-patches@gcc.gnu.org Cc: hubicka@ucw.cz, rguenther@suse.de Subject: [PATCH 3/7 v2] Lockfile. Message-ID: References: <18cc1c3980551ac1881eea6e78811a629c7baa82.1700222403.git.mjires@suse.cz> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: X-Spamd-Result: default: False [-4.01 / 50.00]; BAYES_HAM(-3.00)[100.00%]; NEURAL_HAM_LONG(-1.00)[-1.000]; MID_RHS_NOT_FQDN(0.50)[]; R_DKIM_ALLOW(-0.20)[suse.cz:s=susede2_rsa,suse.cz:s=susede2_ed25519]; NEURAL_HAM_SHORT(-0.20)[-1.000]; MIME_GOOD(-0.10)[text/plain]; MX_GOOD(-0.01)[]; RCVD_VIA_SMTP_AUTH(0.00)[]; RBL_SPAMHAUS_BLOCKED_OPENRESOLVER(0.00)[2a07:de40:b281:104:10:150:64:97:from]; RCVD_TLS_ALL(0.00)[]; MISSING_XM_UA(0.00)[]; ARC_NA(0.00)[]; SPAMHAUS_XBL(0.00)[2a07:de40:b281:104:10:150:64:97:from]; MIME_TRACE(0.00)[0:+]; RECEIVED_SPAMHAUS_BLOCKED_OPENRESOLVER(0.00)[2a07:de40:b281:106:10:150:64:167:received]; DBL_BLOCKED_OPENRESOLVER(0.00)[imap1.dmz-prg2.suse.org:helo,imap1.dmz-prg2.suse.org:rdns,suse.cz:dkim]; RCPT_COUNT_THREE(0.00)[3]; FROM_EQ_ENVFROM(0.00)[]; FROM_HAS_DN(0.00)[]; FUZZY_BLOCKED(0.00)[rspamd.com]; RCVD_COUNT_TWO(0.00)[2]; TO_MATCH_ENVRCPT_ALL(0.00)[]; TO_DN_NONE(0.00)[]; DKIM_SIGNED(0.00)[suse.cz:s=susede2_rsa,suse.cz:s=susede2_ed25519]; DKIM_TRACE(0.00)[suse.cz:+] X-Rspamd-Action: no action X-Rspamd-Server: rspamd2.dmz-prg2.suse.org X-Rspamd-Queue-Id: 8435D1F455 X-Spam-Score: -4.01 X-Spam-Level: X-Spam-Status: No, score=-11.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org This version differs by using INCLUDE_STRING instead of . (+whitespace and year) diff --git a/gcc/Makefile.in b/gcc/Makefile.in index f5adb647d3f..90ec59dca75 100644 --- a/gcc/Makefile.in +++ b/gcc/Makefile.in @@ -1855,7 +1855,7 @@ ALL_HOST_BACKEND_OBJS = $(GCC_OBJS) $(OBJS) $(OBJS-libcommon) \ $(OBJS-libcommon-target) main.o c-family/cppspec.o \ $(COLLECT2_OBJS) $(EXTRA_GCC_OBJS) $(GCOV_OBJS) $(GCOV_DUMP_OBJS) \ $(GCOV_TOOL_OBJS) $(GENGTYPE_OBJS) gcc-ar.o gcc-nm.o gcc-ranlib.o \ - lto-wrapper.o collect-utils.o + lto-wrapper.o collect-utils.o lockfile.o # for anything that is shared use the cc1plus profile data, as that # is likely the most exercised during the build @@ -2384,7 +2384,8 @@ collect2$(exeext): $(COLLECT2_OBJS) $(LIBDEPS) CFLAGS-collect2.o += -DTARGET_MACHINE=\"$(target_noncanonical)\" \ @TARGET_SYSTEM_ROOT_DEFINE@ -LTO_WRAPPER_OBJS = lto-wrapper.o collect-utils.o ggc-none.o +LTO_WRAPPER_OBJS = lto-wrapper.o collect-utils.o ggc-none.o lockfile.o + lto-wrapper$(exeext): $(LTO_WRAPPER_OBJS) libcommon-target.a $(LIBDEPS) +$(LINKER) $(ALL_LINKERFLAGS) $(LDFLAGS) -o T$@ \ $(LTO_WRAPPER_OBJS) libcommon-target.a $(LIBS) diff --git a/gcc/lockfile.cc b/gcc/lockfile.cc new file mode 100644 index 00000000000..8ecb4dc2848 --- /dev/null +++ b/gcc/lockfile.cc @@ -0,0 +1,136 @@ +/* File locking. + Copyright (C) 2023-2024 Free Software Foundation, Inc. + +This file is part of GCC. + +GCC is free software; you can redistribute it and/or modify it under +the terms of the GNU General Public License as published by the Free +Software Foundation; either version 3, or (at your option) any later +version. + +GCC is distributed in the hope that it will be useful, but WITHOUT ANY +WARRANTY; without even the implied warranty of MERCHANTABILITY or +FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +for more details. + +You should have received a copy of the GNU General Public License +along with GCC; see the file COPYING3. If not see +. */ + +#define INCLUDE_STRING +#include "config.h" +#include "system.h" +#include "lockfile.h" + + +/* Unique write lock. No other lock can be held on this lockfile. + Blocking call. */ +int +lockfile::lock_write () +{ + fd = open (filename.c_str (), O_RDWR | O_CREAT, 0666); + if (fd < 0) + return -1; + +#if HAVE_FCNTL_H + struct flock s_flock; + + s_flock.l_whence = SEEK_SET; + s_flock.l_start = 0; + s_flock.l_len = 0; + s_flock.l_pid = getpid (); + s_flock.l_type = F_WRLCK; + + while (fcntl (fd, F_SETLKW, &s_flock) && errno == EINTR) + continue; +#endif + return 0; +} + +/* Unique write lock. No other lock can be held on this lockfile. + Only locks if this filelock is not locked by any other process. + Return whether locking was successful. */ +int +lockfile::try_lock_write () +{ + fd = open (filename.c_str (), O_RDWR | O_CREAT, 0666); + if (fd < 0) + return -1; + +#if HAVE_FCNTL_H + struct flock s_flock; + + s_flock.l_whence = SEEK_SET; + s_flock.l_start = 0; + s_flock.l_len = 0; + s_flock.l_pid = getpid (); + s_flock.l_type = F_WRLCK; + + if (fcntl (fd, F_SETLK, &s_flock) == -1) + { + close (fd); + fd = -1; + return 1; + } +#endif + return 0; +} + +/* Shared read lock. Only read lock can be held concurrently. + If write lock is already held by this process, it will be + changed to read lock. + Blocking call. */ +int +lockfile::lock_read () +{ + fd = open (filename.c_str (), O_RDWR | O_CREAT, 0666); + if (fd < 0) + return -1; + +#if HAVE_FCNTL_H + struct flock s_flock; + + s_flock.l_whence = SEEK_SET; + s_flock.l_start = 0; + s_flock.l_len = 0; + s_flock.l_pid = getpid (); + s_flock.l_type = F_RDLCK; + + while (fcntl (fd, F_SETLKW, &s_flock) && errno == EINTR) + continue; +#endif + return 0; +} + +/* Unlock all previously placed locks. */ +void +lockfile::unlock () +{ + if (fd < 0) + { +#if HAVE_FCNTL_H + struct flock s_flock; + + s_flock.l_whence = SEEK_SET; + s_flock.l_start = 0; + s_flock.l_len = 0; + s_flock.l_pid = getpid (); + s_flock.l_type = F_UNLCK; + + fcntl (fd, F_SETLK, &s_flock); +#endif + close (fd); + fd = -1; + } +} + +/* Are lockfiles supported? */ +bool +lockfile::lockfile_supported () +{ +#if HAVE_FCNTL_H + return true; +#else + return false; +#endif +} diff --git a/gcc/lockfile.h b/gcc/lockfile.h new file mode 100644 index 00000000000..c222eb6d3b6 --- /dev/null +++ b/gcc/lockfile.h @@ -0,0 +1,78 @@ +/* File locking. + Copyright (C) 2023-2024 Free Software Foundation, Inc. + +This file is part of GCC. + +GCC is free software; you can redistribute it and/or modify it under +the terms of the GNU General Public License as published by the Free +Software Foundation; either version 3, or (at your option) any later +version. + +GCC is distributed in the hope that it will be useful, but WITHOUT ANY +WARRANTY; without even the implied warranty of MERCHANTABILITY or +FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +for more details. + +You should have received a copy of the GNU General Public License +along with GCC; see the file COPYING3. If not see +. */ + +#ifndef LOCKFILE_H +#define LOCKFILE_H + +/* Used to synchronize across multiple processes. */ +class lockfile { +public: + /* Default constructor. */ + lockfile (): fd (-1) + {} + /* Intended constructor for use. Filename should not be used for anything + other than locking to prevent unintentional unlock. */ + lockfile (std::string filename): lockfile () + { + this->filename = std::move (filename); + } + lockfile (lockfile const& o): lockfile (o.filename) + {} + + void operator=(lockfile o) + { + unlock (); + this->filename = o.filename; + this->fd = o.fd; + o.fd = -1; + } + + /* Unique write lock. No other lock can be held on this lockfile. + Blocking call. */ + int lock_write (); + + /* Unique write lock. No other lock can be held on this lockfile. + Only locks if this filelock is not locked by any other process. + Return whether locking was successful. */ + int try_lock_write (); + + /* Shared read lock. Only read lock can be held concurrently. + If write lock is already held by this process, it will be + changed to read lock. + Blocking call. */ + int lock_read (); + + /* Unlock all previously placed locks. */ + void unlock (); + + /* Returns whether any lock is held. */ + bool + locked () + { + return fd < 0; + } + + /* Are lockfiles supported? */ + static bool lockfile_supported (); +private: + std::string filename; + int fd; +}; + +#endif