// Copyright 2022 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package procfs

import (
	
	
	

	fsi 
)

// Provide access to /proc/PID/task/TID files, for thread specific values. Since
// such files have the same structure as /proc/PID/ ones, the data structures
// and the parsers for the latter may be reused.

// AllThreads returns a list of all currently available threads under /proc/PID.
func ( int) (Procs, error) {
	,  := NewFS(DefaultMountPoint)
	if  != nil {
		return Procs{}, 
	}
	return .AllThreads()
}

// AllThreads returns a list of all currently available threads for PID.
func ( FS) ( int) (Procs, error) {
	 := .proc.Path(strconv.Itoa(), "task")
	,  := os.Open()
	if  != nil {
		return Procs{}, 
	}
	defer .Close()

	,  := .Readdirnames(-1)
	if  != nil {
		return Procs{}, fmt.Errorf("%w: could not read %q: %w", ErrFileRead, .Name(), )
	}

	 := Procs{}
	for ,  := range  {
		,  := strconv.ParseInt(, 10, 64)
		if  != nil {
			continue
		}

		 = append(, Proc{PID: int(), fs: FS{fsi.FS(), .isReal}})
	}

	return , nil
}

// Thread returns a process for a given PID, TID.
func ( FS) (,  int) (Proc, error) {
	 := .proc.Path(strconv.Itoa(), "task")
	if ,  := os.Stat();  != nil {
		return Proc{}, 
	}
	return Proc{PID: , fs: FS{fsi.FS(), .isReal}}, nil
}

// Thread returns a process for a given TID of Proc.
func ( Proc) ( int) (Proc, error) {
	 := FS{fsi.FS(.path("task")), .fs.isReal}
	if ,  := os.Stat(.proc.Path(strconv.Itoa()));  != nil {
		return Proc{}, 
	}
	return Proc{PID: , fs: }, nil
}