---
name: "devvn_refthread"
section: "9"
platform: "freebsd"
package: "base"
version: "16.0-CURRENT-20261027-61c70dc16a79"
release: "FreeBSD 16.0-CURRENT (2026-10-27)"
platforms: ["freebsd"]
url: "https://new.carta.tech/dev_refthread"
---

# devvn\_refthread(9)

safely access device methods

Part of [dev\_refthread(9)](/dev_refthread/9.md).

## NAME

**dev\_refthread**, **devvn\_refthread**, **dev\_relthread** – safely access device methods

## SYNOPSIS

**#include \<sys/param.h\>**

**#include \<sys/conf.h\>**

*struct cdevsw \**\

**dev\_refthread**(*struct cdev \*dev*, *int \*ref*);

*struct cdevsw \**\

**devvn\_refthread**(*struct vnode \*vp*, *struct cdev \*\*devp*, *int \*ref*);

*void*\

**dev\_relthread**(*struct cdev \*dev*, *int ref*);

## DESCRIPTION

The **dev\_refthread**() (or **devvn\_refthread**()) and **dev\_relthread**() routines provide a safe way to access [devfs(4)](/devfs/4.md) devices that may be concurrently destroyed by **destroy\_dev**() (e.g., removable media).

If successful, **dev\_refthread**() and **devvn\_refthread**() acquire a "thread reference" to the associated *struct cdev* and return a non-NULL pointer to the cdev's *struct cdevsw* method table. For the duration of that reference, the cdev's associated private data and method table object are valid. Destruction of the cdev sleeps until the thread reference is released.

A reference cannot prevent media removal. It is an implementation detail of individual drivers how method calls from callers with **dev\_refthread**() references are handled when the device is pending destruction. A common behavior for disk devices is to return the ENXIO status, but that is not required by this KPI.

The **devvn\_refthread**() variant of **dev\_refthread**() extracts the *struct cdev* pointer out of the VCHR [vnode(9)](/vnode/9.md) automatically before performing the same actions as **dev\_refthread**(). Additionally, a pointer to the *struct cdev* is returned to the caller via *\*devp*. **devvn\_refthread**() correctly handles possible parallel reclamation of the vnode.

**dev\_relthread**() is used to release a reference to a *struct cdev*. **dev\_relthread**() **must** only be invoked when the associated invocation of **dev\_refthread**() or **devvn\_refthread**() returned a non-NULL *struct cdevsw \**.

## CONTEXT

*struct* *cdev* objects have two reference counts, *si\_refcount* and *si\_threadcount*. The **dev\_refthread**(), **devvn\_refthread**(), and **dev\_relthread**() functions manipulate the *si\_threadcount*. The *si\_threadcount* reference guarantees the liveness of the *struct* *cdev* object. The other *si\_refcount* reference provides only the weaker guarantee that the memory backing the *struct* *cdev* has not been freed.

## RETURN VALUES

If **dev\_refthread**() or **devvn\_refthread**() are unsuccessful, they return NULL. If these routines are unsuccessful, they do not increment the *struct cdev* *si\_threadcount* and do not initialize the value pointed to by the *\*ref* parameter in any way.

## SEE ALSO

[devfs(4)](/devfs/4.md), [destroy\_dev(9)](/destroy_dev/9.md)

## CAVEATS

Do not invoke **dev\_relthread**() unless the matching refthread routine succeeded!
