Skip to content

Commit

Permalink
affinity: remove darwin implementation and make it no-op
Browse files Browse the repository at this point in the history
It seems it is not possible to actually set affinity on macOS so we make it a
no-op and make all CPU cores available

https://developer.apple.com/forums/thread/44002
  • Loading branch information
aconchillo committed Dec 18, 2022
1 parent 1595cc2 commit f18fd72
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 186 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ build/
/build-aux/depcomp
/build-aux/texinfo.tex
/fibers.info
/fibers/affinity.scm
/fibers/config.scm
/fibers/posix-clocks.scm
/fibers/events-impl.scm
Expand Down
28 changes: 14 additions & 14 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ info_TEXINFOS=fibers.texi

SOURCES = \
fibers.scm \
fibers/affinity.scm \
fibers/channels.scm \
fibers/conditions.scm \
fibers/config.scm \
Expand All @@ -40,7 +39,6 @@ SOURCES = \
fibers/nameset.scm \
fibers/operations.scm \
fibers/psq.scm \
fibers/posix-clocks.scm \
fibers/repl.scm \
fibers/scheduler.scm \
fibers/stack.scm \
Expand All @@ -49,9 +47,10 @@ SOURCES = \
web/server/fibers.scm

BUILT_SOURCES = \
fibers/affinity.scm \
fibers/config.scm \
fibers/posix-clocks.scm \
fibers/events-impl.scm
fibers/events-impl.scm \
fibers/posix-clocks.scm

extlibdir = $(libdir)/guile/$(GUILE_EFFECTIVE_VERSION)/extensions
AM_CFLAGS = -I$(srcdir) $(WARN_CFLAGS) $(DEBUG_CFLAGS)
Expand Down Expand Up @@ -81,13 +80,6 @@ fibers/events-impl.scm: Makefile fibers/epoll.scm
endif
endif

if ! GUILE_HAVE_AFFINITY
extlib_LTLIBRARIES += fibers-affinity.la
fibers_affinity_la_SOURCES = extensions/$(PLATFORM)/affinity.c
fibers_affinity_la_CFLAGS = $(AM_CFLAGS) $(GUILE_CFLAGS) -Iextensions
fibers_affinity_la_LDFLAGS = -module -no-undefined $(GUILE_LDFLAGS)
endif

if ! HAVE_CLOCK_NANOSLEEP
extlib_LTLIBRARIES += fibers-clocks.la
fibers_clocks_la_SOURCES = extensions/$(PLATFORM)/clock-nanosleep.c
Expand All @@ -100,10 +92,17 @@ fibers/config.scm: Makefile fibers/config.scm.in
sed -e "s|@extlibdir\@|$(extlibdir)|" \
$(srcdir)/fibers/config.scm.in > fibers/config.scm

fibers/affinity.scm: Makefile fibers/affinity-$(PLATFORM).scm
cp -f $(abs_top_srcdir)/fibers/affinity-$(PLATFORM).scm $(abs_top_builddir)/fibers/affinity.scm

fibers/posix-clocks.scm: Makefile fibers/posix-clocks-$(PLATFORM).scm
cp -f $(abs_top_srcdir)/fibers/posix-clocks-$(PLATFORM).scm $(abs_top_builddir)/fibers/posix-clocks.scm

CLEANFILES += fibers/config.scm fibers/posix-clocks.scm fibers/events-impl.scm
CLEANFILES += \
fibers/affinity.scm \
fibers/config.scm \
fibers/events-impl.scm \
fibers/posix-clocks.scm

TESTS = \
tests/basic.scm \
Expand Down Expand Up @@ -136,6 +135,8 @@ EXTRA_DIST += \
fibers/config.scm.in \
fibers/epoll.scm \
fibers/libevent.scm \
fibers/affinity-darwin.scm \
fibers/affinity-generic.scm \
fibers/posix-clocks-darwin.scm \
fibers/posix-clocks-generic.scm \
examples
Expand All @@ -144,7 +145,6 @@ EXTRA_DIST += \
EXTRA_DIST += \
extensions/epoll.c \
extensions/libevent.c \
extensions/darwin/affinity.c \
extensions/clock-nanosleep.h \
extensions/darwin/clock-nanosleep.c \
extensions/generic/affinity.c \
extensions/generic/clock-nanosleep.c
3 changes: 0 additions & 3 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,6 @@ AC_ARG_ENABLE([epoll], AS_HELP_STRING([--disable-epoll],[Disable epoll support])
AC_CHECK_FUNCS(epoll_wait)
AM_CONDITIONAL([HAVE_EPOLL_WAIT], [test "x$ac_cv_func_epoll_wait" = "xyes"])

GUILE_CHECK([GUILE_HAVE_AFFINITY], (exit (or (defined? 'setaffinity) 2)))
AM_CONDITIONAL([GUILE_HAVE_AFFINITY], [test "x$GUILE_HAVE_AFFINITY" = "x0"])

AC_CHECK_FUNCS(clock_nanosleep)
AM_CONDITIONAL([HAVE_CLOCK_NANOSLEEP], [test "x$ac_cv_func_clock_nanosleep" = "xyes"])

Expand Down
153 changes: 0 additions & 153 deletions extensions/darwin/affinity.c

This file was deleted.

4 changes: 0 additions & 4 deletions extensions/generic/affinity.c

This file was deleted.

37 changes: 37 additions & 0 deletions fibers/affinity-darwin.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
;; CPU affinity (Darwin)

;;;; Copyright (C) 2022 Aleix Conchillo Flaqué <[email protected]>
;;;;
;;;; This library is free software; you can redistribute it and/or
;;;; modify it under the terms of the GNU Lesser General Public
;;;; License as published by the Free Software Foundation; either
;;;; version 3 of the License, or (at your option) any later version.
;;;;
;;;; This library 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
;;;; Lesser General Public License for more details.
;;;;
;;;; You should have received a copy of the GNU Lesser General Public License
;;;; along with this program. If not, see <http://www.gnu.org/licenses/>.

;;; Guile defines setaffinity and getaffinity in some systems (e.g. Linux). For
;;; those systems where those procedures are not available there should be a
;;; Fibers' specific implementation available through the fibers-affinity
;;; library.

(define-module (fibers affinity)
#:use-module (ice-9 threads)
#:export (getaffinity* setaffinity*))

;;
;; It seems it is not possible to link a thread to a specific core on
;; macOS. See, for example: https://developer.apple.com/forums/thread/44002.
;;
;; So for now getaffinity/setaffinity are no-ops.
;;

(define (getaffinity* pid)
(make-bitvector (current-processor-count) 1))

(define (setaffinity* pid affinity) *unspecified*)
13 changes: 1 addition & 12 deletions fibers/affinity.scm → fibers/affinity-generic.scm
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,6 @@
#:use-module (fibers config)
#:export (getaffinity* setaffinity*))

(eval-when (eval load compile)
(unless (defined? 'getaffinity)
;; When cross-compiling, the cross-compiled 'fibers-affinity.so' cannot be
;; loaded by the 'guild compile' process; skip it.
(unless (getenv "FIBERS_CROSS_COMPILING")
(catch #t
(lambda ()
(dynamic-call "init_fibers_affinity" (dynamic-link (extension-library "fibers-affinity"))))
(lambda _ (error "Ooops, getaffinity/setaffinity are not available in this platform and we were \
unable to load fibers-affinity extension."))))))

;; getaffinity/setaffinity should be loaded at this point.
;; getaffinity/setaffinity should be defined in Guile
(define getaffinity* (if (defined? 'getaffinity) getaffinity))
(define setaffinity* (if (defined? 'setaffinity) setaffinity))

0 comments on commit f18fd72

Please sign in to comment.