From nobody Tue Jul 23 16:09:11 2024 X-Original-To: freebsd-ports@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4WT2CG3PbNz5S9Xr for ; Tue, 23 Jul 2024 16:09:14 +0000 (UTC) (envelope-from SRS0=ZlwR=OX=klop.ws=ronald-lists@realworks.nl) Received: from smtp-relay-int.realworks.nl (smtp-relay-int.realworks.nl [194.109.157.24]) (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) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 4WT2CG14d2z4b5M; Tue, 23 Jul 2024 16:09:14 +0000 (UTC) (envelope-from SRS0=ZlwR=OX=klop.ws=ronald-lists@realworks.nl) Authentication-Results: mx1.freebsd.org; none Date: Tue, 23 Jul 2024 18:09:11 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=klop.ws; s=rw2; t=1721750952; h=from:from:reply-to:subject:subject: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=y0x7BgFuu3AQAM7y0tq+K8kwoC4ASKGbooGHdzYbp64=; b=OjJdgs0qPq/w2iYpmuiJ/pgKXRk9d81s7pFRY+vmIkC/r5jCNYnl7DqD0ueBX/NikzDqzH XNiTNIzs5o4bSKRWcl080tCQxF6RsMVK2xYnTaTcpr7QFjKOHDdQ97DrFU/QcppPAq/IAs MaNjtpqh7JZ39q1H/VFbbiA6zKiEwuLohuTOCGzGxg8zEsX7rP6wi+Tkh8YMe84mbAZwPy eG/Ac4BP+j31r0QExEwTv1mvTdRqup8DIAkTBgh84bo63hhbSYmT1b8X4QvJxd2mwXFFQX 1KdOhd4GmMoEi7h772XwNio17G1fO71s64UXZd9AoEvBUEJ7GEBxRIxljijU9A== From: Ronald Klop To: freebsd-ports@freebsd.org Cc: Michael Gmelin Message-ID: <649335513.7189.1721750951946@localhost> In-Reply-To: References: <1711627700.7093.1721744204825@localhost> Subject: Re: advice for implementing an atomics wrapper List-Id: Porting software to FreeBSD List-Archive: https://lists.freebsd.org/archives/freebsd-ports List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: freebsd-ports@freebsd.org Sender: owner-freebsd-ports@FreeBSD.org MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_Part_7188_792930881.1721750951921" X-Mailer: Realworks (711.17) Importance: Normal X-Priority: 3 (Normal) X-Spamd-Bar: ---- X-Rspamd-Pre-Result: action=no action; module=replies; Message is reply to one we originated X-Spamd-Result: default: False [-4.00 / 15.00]; REPLY(-4.00)[]; ASN(0.00)[asn:3265, ipnet:194.109.0.0/16, country:NL] X-Rspamd-Queue-Id: 4WT2CG14d2z4b5M ------=_Part_7188_792930881.1721750951921 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit Hi, The __APPLE__ implementation also pointed to the implementation in that file and I used it to come up with a patch. #elif defined(__FreeBSD__) void notifyOne(const void* uaddr) { _umtx_op(const_cast(uaddr), UMTX_OP_WAKE, 1, NULL, NULL); } void notifyMany(const void* uaddr, int nToWake) { _umtx_op(const_cast(uaddr), UMTX_OP_WAKE, nToWake, NULL, NULL); } void notifyAll(const void* uaddr) { _umtx_op(const_cast(uaddr), UMTX_OP_WAKE, INT_MAX, NULL, NULL); } bool waitUntil(const void* uaddr, uint32_t old, boost::optional deadline) { struct timespec timeout; bool timeoutOverflow = false; if (deadline) { int64_t micros = durationCount(*deadline - system_clock::now()); if (micros <= 0) { return false; // Synthesize a timeout. } if (micros > int64_t(std::numeric_limits::max())) { // 2**32 micros is a little over an hour. If this happens, we wait as long as we can, // then return as-if a spurious wakeup happened, rather than a timeout. This will cause // the caller to loop and we will compute a smaller time each pass, eventually reaching // a representable timeout. micros = std::numeric_limits::max(); timeoutOverflow = true; } timeout.tv_sec = micros / 1000; timeout.tv_nsec = (micros % 1000) * 1000; } if (_umtx_op(const_cast(uaddr), UMTX_OP_WAIT, old, (void*)sizeof(struct timespec), &timeout) != -1) return true; // There isn't a good list of possible errors, so assuming that anything other than a timeout // error is a possible spurious wakeup. return timeoutOverflow || errno != ETIMEDOUT; } It compiles and runs in my simple tests (although I don't know if my simple tests execute this part of mongodb code). Suggestions are of course welcome. Regards, Ronald. Van: Michael Gmelin Datum: dinsdag, 23 juli 2024 17:32 Aan: Ronald Klop Onderwerp: Re: advice for implementing an atomics wrapper > > > Does this help? > > https://reviews.llvm.org/D142134 > > >> On 23. Jul 2024, at 16:17, Ronald Klop wrote: >> > >> >> Sorry, >> >> Something broke the pasted URL. >> https://github.com/mongodb/mongo/blob/master/src/mongo/platform/waitable_atomic.cpp >> >> Another attempt. >> >> Ronald. >> >> Van: Ronald Klop >> Datum: dinsdag, 23 juli 2024 16:14 >> Aan: freebsd-ports@freebsd.org >> Onderwerp: advice for implementing an atomics wrapper >>> >>> Hi, >>> >>> For mongodb 8.0 I need to give a FreeBSD implementation of this file. >>> >>> https://github.com/mongodb/mongo/blob/master/src/mongo/platform/waitable_atomic.cpp >>> >>> Using the __Apple__ definition gives: >>> ld.lld: error: undefined symbol: __ulock_wake >>> ld.lld: error: undefined symbol: __ulock_wait >>> >>> And the __linux__ definition uses futex which FreeBSD does not have AFAIK. >>> >>> What is an easy way to port this file? >>> >>> Regards, >>> Ronald. >>> >> >> > ------=_Part_7188_792930881.1721750951921 Content-Type: text/html; charset=us-ascii Content-Transfer-Encoding: 7bit Hi,

The __APPLE__ implementation also pointed to the implementation in that file and I used it to come up with a patch.

#elif defined(__FreeBSD__)

void notifyOne(const void* uaddr) {
    _umtx_op(const_cast<void*>(uaddr), UMTX_OP_WAKE, 1, NULL, NULL);
}

void notifyMany(const void* uaddr, int nToWake) {
    _umtx_op(const_cast<void*>(uaddr), UMTX_OP_WAKE, nToWake, NULL, NULL);
}

void notifyAll(const void* uaddr) {
    _umtx_op(const_cast<void*>(uaddr), UMTX_OP_WAKE, INT_MAX, NULL, NULL);
}

bool waitUntil(const void* uaddr,
               uint32_t old,
               boost::optional<system_clock::time_point> deadline) {
    struct timespec timeout;
    bool timeoutOverflow = false;
    if (deadline) {
        int64_t micros = durationCount<Microseconds>(*deadline - system_clock::now());
        if (micros <= 0) {
            return false;  // Synthesize a timeout.
        }

        if (micros > int64_t(std::numeric_limits<uint32_t>::max())) {
            // 2**32 micros is a little over an hour. If this happens, we wait as long as we can,
            // then return as-if a spurious wakeup happened, rather than a timeout. This will cause
            // the caller to loop and we will compute a smaller time each pass, eventually reaching
            // a representable timeout.
            micros = std::numeric_limits<uint32_t>::max();
            timeoutOverflow = true;
        }

        timeout.tv_sec = micros / 1000;
        timeout.tv_nsec = (micros % 1000) * 1000;
    }

    if (_umtx_op(const_cast<void*>(uaddr), UMTX_OP_WAIT, old, (void*)sizeof(struct timespec), &timeout) != -1)
        return true;

    // There isn't a good list of possible errors, so assuming that anything other than a timeout
    // error is a possible spurious wakeup.
    return timeoutOverflow || errno != ETIMEDOUT;
}


It compiles and runs in my simple tests (although I don't know if my simple tests execute this part of mongodb code). Suggestions are of course welcome.

Regards,
Ronald.

 

Van: Michael Gmelin <grembo@freebsd.org>
Datum: dinsdag, 23 juli 2024 17:32
Aan: Ronald Klop <ronald-lists@klop.ws>
Onderwerp: Re: advice for implementing an atomics wrapper

 
Does this help?
 
 
 
On 23. Jul 2024, at 16:17, Ronald Klop <ronald-lists@klop.ws> wrote:
 
Sorry,

Something broke the pasted URL.
https://github.com/mongodb/mongo/blob/master/src/mongo/platform/waitable_atomic.cpp

Another attempt.

Ronald.
 

Van: Ronald Klop <ronald-lists@klop.ws>
Datum: dinsdag, 23 juli 2024 16:14
Aan: freebsd-ports@freebsd.org
Onderwerp: advice for implementing an atomics wrapper

Hi,

For mongodb 8.0 I need to give a FreeBSD implementation of this file.

https://github.com/mongodb/mongo/blob/master/src/mongo/platform/waitable_atomic.cpp

Using the __Apple__ definition gives:
ld.lld: error: undefined symbol: __ulock_wake
ld.lld: error: undefined symbol: __ulock_wait

And the __linux__ definition uses futex which FreeBSD does not have AFAIK.

What is an easy way to port this file?

Regards,
Ronald.
 

 

  ------=_Part_7188_792930881.1721750951921--